diff --git a/unitool.js b/unitool.js index 480cefb..f6141ed 100644 --- a/unitool.js +++ b/unitool.js @@ -492,10 +492,11 @@ export default { // #endif }, + // precision 要有默认值,以防无法连接后台时,这个方法会导致 part-header.vue 出错。 formatMoney (amount, { precision = 2 } = {}) { + // parseInt(NaN/undefined/false/null/'') 都返回 NaN,而 Number(false/null/'')===0,因此用 parseInt 来过滤无效输入。 + // 或者可以 if (!['number', 'string'].includes(typeof amount) && [NaN, undefined, false, null, ''].includes(amount)) 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 虽然方便,但是会自动四舍五入。 @@ -504,12 +505,10 @@ export default { 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 出错。 + precision = wo.envarRemote?.pexPrecision } return `${parseInt(Number(amount) * Math.pow(10, precision)) / Math.pow(10, precision)}` },