update formatMoney and format_coin

This commit is contained in:
陆柯 2022-10-06 15:15:03 +08:00
parent a6cada784f
commit 9d766b30a8

View File

@ -492,8 +492,26 @@ export default {
// #endif
},
formatMoney (value, precision = 2) {
return Number(value || 0).toFixed(precision) // Number(undefined)===NaN
formatMoney (amount, { precision = 2 } = {}) {
if (Number.isNaN(parseInt(amount))) {
// parseInt(NaN/undefined/false/null/'') 都返回 NaN而 Number(false/null/'')===0因此用 parseInt 来过滤无效输入。
// 或者可以 if (!['number', 'string'].includes(typeof amount) && [NaN, undefined, false, null, ''].includes(amount))
return ''
}
// Number(amount).toFixed(precision) // toFixed 虽然方便,但是会自动四舍五入。
return `${parseInt(Number(amount) * Math.pow(10, precision)) / Math.pow(10, precision)}`
},
format_coin (amount, { coin = wo.envar.KEYNAME, precision = 8 } = {}) {
if (Number.isNaN(parseInt(amount))) {
// parseInt(NaN/undefined/false/null/'') 都返回 NaN而 Number(false/null/'')===0因此用 parseInt 来过滤无效输入。
// 或者可以 if (!['number', 'string'].includes(typeof amount) && [NaN, undefined, false, null, ''].includes(amount))
return ''
}
if (coin === wo.envar.KEYNAME && wo.envarRemote?.pexPrecision) {
precision = wo.envarRemote?.pexPrecision // precision 要有默认值,以防无法连接后台时,这个方法会导致 part-header.vue 出错。
}
return `${parseInt(Number(amount) * Math.pow(10, precision)) / Math.pow(10, precision)}`
},
formatPercent (value, precision = 2) {