生活随笔
收集整理的這篇文章主要介紹了
js实现千分位分割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
方法一:
function toThousands(num) {let [integer, decimal] = String.prototype.split.call(num, '.');integer = (integer || 0).toString();let result = '';while (integer.length > 3) {result = ',' + integer.slice(-3) + result;integer = integer.slice(0, integer.length - 3);}if (integer) { result = integer + result; }return `${result}${decimal ? '.' + decimal : '' }`;
}
方法二:
function toThousands(num) {num = parseFloat(num.toFixed(3));let [integer, decimal] = String.prototype.split.call(num, '.');integer = integer.replace(/\d(?=(\d{3})+$)/g, '$&,'); // 正則先行斷言return `${integer}${decimal ? '.' + decimal : ''}`;
}
總結
以上是生活随笔為你收集整理的js实现千分位分割的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。