From 0cc0e754e41a126bf6384ebf537ca1c3281728f3 Mon Sep 17 00:00:00 2001 From: fengachen <373943794@qq.com> Date: Thu, 2 Jun 2022 13:44:30 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=94=B9=E3=80=91=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/formatMoney.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/utils/formatMoney.js b/src/utils/formatMoney.js index e523d2b..da07259 100644 --- a/src/utils/formatMoney.js +++ b/src/utils/formatMoney.js @@ -3,11 +3,13 @@ export function formatMoney(money) { re = /\d{3}$/, result = '' var point = '' + // 存在小数点 分开整数和小数点 if (num.indexOf('.') !== -1) { point = num.substring(num.indexOf('.')) num = parseInt(num) - } + + // 循环添加逗号 while (re.test(num)) { result = RegExp.lastMatch + result if (num !== RegExp.lastMatch) { @@ -18,19 +20,25 @@ export function formatMoney(money) { break } } + if (num) { result = num + result } + // 去掉负数符号后面的逗号 function _(result) { return result.replace('-,', '-') } + + // 如果第一位是逗号 去掉逗号 function __(result) { if (result[0] === ',') { result = result.substring(1) } return result } + + // 第一位是 小数点开头的 前面加上 0 function ___(result) { if (result[0] === '.') { result = '0' + result