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'

猜你喜欢

转载自www.cnblogs.com/wwwweb/p/8919447.html