From 9d766b30a8694ad2056e8ddf274374fb3deb8e2a Mon Sep 17 00:00:00 2001 From: Luk Lu Date: Thu, 6 Oct 2022 15:15:03 +0800 Subject: [PATCH] update formatMoney and format_coin --- unitool.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/unitool.js b/unitool.js index cf3abd8..480cefb 100644 --- a/unitool.js +++ b/unitool.js @@ -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) {