From 91cc8feefa2abd9eefb2d99eb6815663e046fa8b Mon Sep 17 00:00:00 2001 From: yeweikangxx123 <317226901@qq.com> Date: Wed, 23 Jul 2025 23:43:06 +0800 Subject: [PATCH] =?UTF-8?q?fix(util):=20=E7=B2=BE=E5=BA=A6=E9=97=AE?= =?UTF-8?q?=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/index.js | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/util/index.js b/util/index.js index 4d5f3e4..815cfb5 100644 --- a/util/index.js +++ b/util/index.js @@ -81,10 +81,37 @@ function formatSeconds(second_time) { function formatCurrency(value) { // 处理空值或无效值 - if (!value) { - return '0.00' + if (value === null || value === undefined || value === '') { + return value + } + + // 转换为数字类型 + const numValue = typeof value === 'string' ? parseFloat(value) : value + + // 验证是否为有效数字 + if (isNaN(numValue)) { + return value + } + + // 截断到两位小数(不四舍五入) + const truncated = Math.floor(numValue * 100) / 100 + + // 添加千分位分隔符的函数 + function addThousandSeparator(num) { + return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') + } + + // 判断是否有小数部分 + if (truncated % 1 === 0) { + // 整数,不显示小数位,但添加千分位分隔符 + return addThousandSeparator(numValue) + } else { + // 有小数,保留两位,并为整数部分添加千分位分隔符 + const fixedValue = numValue.toFixed(2) + const parts = fixedValue.split('.') + parts[0] = addThousandSeparator(parts[0]) + return parts.join('.') } - return value } export { formatCurrency, formatMsToDate, formatSeconds, isEmpty }