业务需求:金额在千分位要加上,并且保留两位小数
话不多说,直接上代码
金额千分位添加
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22export function format_number(s) { if (s == null || s == '' || s == 0) { return '0' } s = s.toString().replace(/^(d*)$/, '$1.') s = s.replace(/(d*.dd)d*/, '$1') if (s > 0) { s = (s + '00').replace(/(d*.dd)d*/, '$1') } else if (s < 0) { if (String(s).indexOf('.') < 0) { s = (s + '.00').replace(/(d*.dd)d*/, '$1') } else { s = (s + '00').replace(/(d*.dd)d*/, '$1') } } s = s.replace('.', ',') var re = /(d)(d{3},)/ while (re.test(s)) { s = s.replace(re, '$1,$2') } s = s.replace(/,(dd)$/, '.$1') return s }
金额移除千分位
复制代码
1
2
3
4
5
6
7
8
9
10
11export function moneyDelete(num) { if (num && num != undefined && num != null || num == 0) { let _num = num _num = _num.toString() _num = _num.replace(/,/gi, '') return _num } else { return num } }
经本人测试,完全有效,且没有bug。如果各位大佬有更好的方式方法,欢迎留言赐教,谢谢!!!
最后
以上就是乐观草莓最近收集整理的关于金额千分位添加,方法以及金额去除千分位,方法的全部内容,更多相关金额千分位添加内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复