Commit ac3fd4a8 by yuzhenWang

修改出账检核

parent 37963fe8
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
"axios": "1.9.0", "axios": "1.9.0",
"clipboard": "2.0.11", "clipboard": "2.0.11",
"dayjs": "^1.11.18", "dayjs": "^1.11.18",
"decimal.js": "^10.6.0",
"echarts": "5.6.0", "echarts": "5.6.0",
"element-plus": "^2.13.5", "element-plus": "^2.13.5",
"file-saver": "^2.0.5", "file-saver": "^2.0.5",
......
import Decimal from 'decimal.js'
// 格式化金额为货币格式 // 格式化金额为货币格式
export function formatCurrency(value, currency = '', fixedDigits = 2) { export function formatCurrency(value, currency = '', fixedDigits = 2) {
if (value === undefined || value === null) return currency + '0.00' if (value === undefined || value === null) return currency + '0.00'
...@@ -206,11 +207,41 @@ export function inputThousands(value) { ...@@ -206,11 +207,41 @@ export function inputThousands(value) {
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',') parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',')
return parts.join('.') return parts.join('.')
} }
/**
* 金额计算(乘/除),保留指定位数小数(四舍五入)
* @param {number|string|Decimal} a - 被乘数(或被除数)
* @param {number|string|Decimal} b - 乘数(或除数)
* @param {number} digits - 小数位数(默认2)
* @param {string} [type] - 'Divided' 表示除法,否则乘法
* @returns {number|null} 计算结果(数字类型)
*/
export function calculateAmount(a, b, digits = 2, type) {
if (a == null || b == null) return null
try {
const da = new Decimal(a)
const db = new Decimal(b)
let result
if (type === 'Divided') {
if (db.isZero()) return null // 除零保护
result = da.dividedBy(db)
} else {
result = da.times(db)
}
// 四舍五入到指定位数,并转为 Number(若需要字符串可用 .toFixed(digits))
// return result.toDecimalPlaces(digits, Decimal.ROUND_HALF_UP).toNumber()
console.log('====================================')
console.log('金钱', result.toFixed(digits, Decimal.ROUND_HALF_UP))
console.log('====================================')
return result.toFixed(digits, Decimal.ROUND_HALF_UP)
} catch (e) {
return null
}
}
export default { export default {
formatCurrency, formatCurrency,
numberFormat, numberFormat,
formatThousands, formatThousands,
formatThousandsSimple, formatThousandsSimple,
inputThousands inputThousands,
calculateAmount
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment