[英]Fix Javascript Value to 0 decimals
我有這個腳本,它正在格式化我的貨幣,但現在它沒有將我的值固定到小數點后 0 位,我是 javascript 的新手,所以有人可以解釋我在哪里以及如何做這個嗎? 謝謝
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;
}
我可能誤解了你的問題,但聽起來你想要 Math.floor()
你可以在數學上對數字進行四舍五入,這要快得多,而且要簡單得多。 僅在這個網站上就有數百個這種算法的例子。
function round(number, places)
{
var multiplicator = Math.pow(10, places);
return Math.round(number * multiplicator) / multiplicator;
}
alert(round(123.456, 0)); // alerts "123"
此 function 可讓您四舍五入到任意小數位。 如果你想四舍五入到最近的 integer,你可以簡單地使用Math.round(x)
。
Math.round
將十進制數四舍五入到最接近的 integer。 假設您想保留3位小數,您所要做的就是將小數乘以 1000(即 10 次方3 ),四舍五入到最接近的 integer,然后除以 1000。這就是此代碼段的作用。
您的 function 似乎對我有用......我假設將"1000"
作為參數傳遞的結果應該是"1,000"
? 我不確定我是否理解您所說的“將 [您的] 值固定為 0 位小數”是什么意思。
這是我寫的 function 格式數字(就像你上面的 function 所做的那樣),用於 API 一次:
function num_format(str) {
if (typeof str !== 'string' || /[^\d,\.e+-]/.test(str)) {
if (typeof str === 'number') {
str = str.toString();
} else {
throw new TypeError("Argument is not a string with a number in it");
}
}
var reg = /(\d+)(\d{3})/;
str = str.split(".");
while (reg.test(str[0])) {
str[0] = str[0].replace(reg, "$1,$2");
}
return str.join(".");
}
刪除錯誤檢查代碼,它變成了一個很短的小 function(五行)。 傳遞給它任何數字,它會正確格式化它,盡管我不允許指定分隔符的選項。
至於將數字四舍五入到任意小數位,我建議在上面查看zneak 的答案。 然后會簡單地做:
num_format(round("100000.12341234", 2));
這將給出"100,000.12"
的結果。
這個 function 應該能夠應對所有各種場景:
function formatNumber(num, precision, sep, thousand, addTrailing0) {
if (isNaN(num))
return "";
if (isNaN(precision))
precision = 2;
if (typeof addTrailing0 == "undefined")
addTrailing0 = true;
var factor = Math.pow(10, precision);
num = String(Math.round(num * factor) / factor);
if (addTrailing0 && precision > 0) {
if (num.indexOf(".") < 0)
num += ".";
for (var length = num.substr(num.indexOf(".")+1).length; length < precision; ++length)
num += "0";
}
var parts = num.split(".");
parts[0] = parts[0].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1" + (thousand || ",")).split("").reverse().join("");
num = parts[0] + (parts.length > 1 ? (sep || ".") + parts[1] : "");
//debug:
document.write(num + "<br />");
return num;
}
例子:
formatNumber(32432342342.3574, 0); // 32,432,342,342
formatNumber(32432342342.3574, 2); // 32,432,342,342.36
formatNumber(1342.525423, 4, ".", ","); // 1,342.5254
formatNumber(1342.525423, 2, ",", "."); // 1.342,53
formatNumber(1342.525423); // 1,342.53
formatNumber(1342.525423, 0); // 1,343
formatNumber(342.525423, 8); // 342.52542300
formatNumber(42.525423, 8, null, null, false); // 42.525423
formatNumber(2.5, 3, ",", "."); // 2,500
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.