From 92bc66d9d672b6890e09cea07b764446a6099082 Mon Sep 17 00:00:00 2001 From: woody Date: Fri, 15 Aug 2025 09:10:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(numberToCurrency):=20=20=E8=B4=A7=E5=B8=81?= =?UTF-8?q?=E7=AC=A6=E5=8F=B7=E5=86=99=E6=AD=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/numberToCurrency.js | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/util/numberToCurrency.js b/util/numberToCurrency.js index 39a7158..4eb8917 100644 --- a/util/numberToCurrency.js +++ b/util/numberToCurrency.js @@ -1,30 +1,34 @@ -import store from '@/store' let userInfo = uni.getStorageSync('User') export function stateFormat(row, column, cellValue) { if (cellValue) { return Number(cellValue) .toFixed(2) .replace(/(\d)(?=(\d{3})+\.)/g, ($0, $1) => { - - return $1 + ','; + return $1 + ',' }) - .replace(/\.$/, ""); + .replace(/\.$/, '') } } //element 转成千分位并保留小数 //decimal 默认保留2位小数 export function toThousandthAndKeepDecimal(element, decimal = 2) { - if (!element || element === '') {//值不存在 或为空 + if (!element || element === '') { + //值不存在 或为空 return '' } else if (typeof element == 'string') { - return Number(element).toLocaleString(undefined, { minimumFractionDigits: decimal, maximumFractionDigits: decimal }); + return Number(element).toLocaleString(undefined, { + minimumFractionDigits: decimal, + maximumFractionDigits: decimal, + }) } else if (typeof element == 'number') { - return element.toLocaleString(undefined, { minimumFractionDigits: decimal, maximumFractionDigits: decimal }); + return element.toLocaleString(undefined, { + minimumFractionDigits: decimal, + maximumFractionDigits: decimal, + }) } else { - return element; + return element } - } // 千分号 export function numberToCurrencyNo(value) { @@ -36,14 +40,17 @@ export function numberToCurrencyNo(value) { // 获取整数部分 const intPart = Math.trunc(value) // 整数部分处理,增加, - const intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') + const intPartFormat = intPart + .toString() + .replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 预定义小数部分 let floatPart = '' // 将数值截取为小数部分和整数部分 const valueArray = value.toString().split('.') - if (valueArray.length === 2) { // 有小数部分 + if (valueArray.length === 2) { + // 有小数部分 floatPart = valueArray[1].toString() // 取得小数部分 - if(floatPart.length == 1){ + if (floatPart.length == 1) { floatPart = floatPart + '0' } return intPartFormat + '.' + floatPart @@ -62,9 +69,9 @@ export function numberToCurrencyNo(value) { // } // 添加当地币 export function isLocal(value) { - return (store.getters.user.currencyIcon + value) || (userInfo.currencyIcon + value) + return '¥' + value } // 添加当地币 export function isLocaled(value) { - return store.getters.user.currencyIcon || userInfo.currencyIcon + return '¥' }