JS方法:数字转换为千分位字符

 

 1 /**
 2  * 数字转为千分位字符
 3  * @param {Number} num
 4  * @param {Number} point 保留几位小数,默认2位
 5  */
 6 function parseToThousandth(num, point = 2) {
 7   let [sInt, sFloat] = (Number.isInteger(num) ? `${num}` : num.toFixed(point)).split('.');
 8   sInt = sInt.replace(/\d(?=(\d{3})+$)/g, '$&,');
 9   return sFloat ? `${sInt}.${sFloat}` : `${sInt}`;
10 }
11 
12 parseToThousandth(1234567); // '1,234,567'

 

转载于:https://www.cnblogs.com/ronffy/p/8919447.html

你可能感兴趣的:(JS方法:数字转换为千分位字符)