分类目录: html5
JS格式化数字(每三位加逗号)
Post date:
Author: cyy
标签: js
Number of comments: no comments
JS格式化数字(每三位加逗号)
方法1:
// 传入数字,超过三位自动补逗号
function thousands(num) {
try {
num = Number(num).toFixed(2); //保留两位小数
var str = num.toString();
var reg = str.indexOf(".") > -1 ? /(\d)(?=(\d{3})+\.)/g : /(\d)(?=(?:\d{3})+$)/g;
return str.replace(reg, "$1,");
} catch (error) {
return num;
}
}
方法2:
数字.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,')