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