是否可以用CSS格式化数字?
即:小数位,小数分隔符,千位分隔符等。
即:小数位,小数分隔符,千位分隔符等。
解决方法
嗯,对于Javascript中的任何数字,我使用下一个:
var a = "1222333444555666777888999"; a = a.replace(new RegExp("^(\\d{" + (a.length%3?a.length%3:0) + "})(\\d{3})","g"),"$1 $2").replace(/(\d{3})+?/gi,"$1 ").trim();
如果您需要使用任何其他分隔符作为逗号,例如:
var sep = ","; a = a.replace(/\s/g,sep);
或作为功能:
function numberFormat(_number,_sep) { _number = typeof _number != "undefined" && _number > 0 ? _number : ""; _number = _number.replace(new RegExp("^(\\d{" + (_number.length%3? _number.length%3:0) + "})(\\d{3})","$1 ").trim(); if(typeof _sep != "undefined" && _sep != " ") { _number = _number.replace(/\s/g,_sep); } return _number; }