你能帮助我改变那个变量,使逗号成为千位分隔符吗?
var number1 = 123456789;
$(function(){
document.getElementById("test").innerHTML("Var number1 is: " + number1); //i want that to display with commas
}
我不理解其他问题的答案。感谢
答案 0 :(得分:0)
你可以在这里看一些答案:
http://www.queness.com/post/9806/5-missing-javascript-number-format-functions
但是这里是同一页面的代码:
function CommaFormatted(amount) {
var delimiter = ","; // replace comma if desired
amount = new String(amount);
var a = amount.split('.',2)
var d = a[1];
var i = parseInt(a[0]);
if(isNaN(i)) { return ''; }
var minus = '';
if(i < 0) { minus = '-'; }
i = Math.abs(i);
var n = new String(i);
var a = [];
while(n.length > 3)
{
var nn = n.substr(n.length-3);
a.unshift(nn);
n = n.substr(0,n.length-3);
}
if(n.length > 0) { a.unshift(n); }
n = a.join(delimiter);
if(d.length < 1) { amount = n; }
else { amount = n + '.' + d; }
amount = minus + amount;
return amount;
}
/**
* Usage: CommaFormatted(12345678);
* result: 12,345,678
**/