目前输出1844.6304
所需的输出 - 点后的逗号数千个修剪(无四舍五入)
1,844
我在论坛上看了一段时间,无法找到解决这两种情况的解决方案。
答案 0 :(得分:1)
它更简单,就像这样
var n = 1844.6304,
s = Math.floor(n).toLocaleString();
console.log(s); //"1,844"
alert(s);
答案 1 :(得分:1)
试试这个:
function intWithCommas(x) {
return Math.floor(x).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
示例:
> intWithCommas(1844.6304)
'1,844'
答案 2 :(得分:0)
试试这个:
function toCommaInteger(number){
var result = "" + ~~number;
var length = result.length;
var limit = result[0] === "-" ? 1 : 0;
for(var i = length-3; i > limit; i-=3 ){
result = result.substring(0,i) + "," + result.substring(i,length);
length++;
}
return result;
}
toCommaInteger(123589.85315) => 123,589
toCommaInteger(-1289.15315) => -1,289
toCommaInteger(5) => 5