我有這個腳本是工作在格式化我的貨幣,但現在它自己的價值不是固定爲0位小數,林新的JavaScript所以可能有人解釋在哪裏和藿我這樣做呢?由於修復JavaScript值到0的小數
function FormatNumberBy3(num, decpoint, sep) {
// check for missing parameters and use defaults if so
if (arguments.length == 2) {
sep = ",";
}
if (arguments.length == 1) {
sep = ",";
decpoint = ".";
}
// need a string for operations
num = num.toString();
// separate the whole number and the fraction if possible
a = num.split(decpoint);
x = a[0]; // decimal
y = a[1]; // fraction
z = "";
if (typeof(x) != "undefined") {
// reverse the digits. regexp works from left to right.
for (i=x.length-1;i>=0;i--)
z += x.charAt(i);
// add seperators. but undo the trailing one, if there
z = z.replace(/(\d{3})/g, "$1" + sep);
if (z.slice(-sep.length) == sep)
z = z.slice(0, -sep.length);
x = "";
// reverse again to get back the number
for (i=z.length-1;i>=0;i--)
x += z.charAt(i);
// add the fraction back in, if it was there
if (typeof(y) != "undefined" && y.length > 0)
x += decpoint + y;
}
return x;
}
你知道'toFixed'(https://開頭developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toFixed)?也許它可以幫助... – 2011-05-25 15:26:10
我用.toFixed但似乎與上面... – Rashid84 2011-05-25 15:27:10