我想要将数字格式化为始终显示两位小数,必要时要四舍五入。
例如:
number display
------ -------
1 1.00
1.341 1.34
1.345 1.35
我一直在使用这个:
parseFloat(num).toFixed(2);
但它显示的是1
,而不是1.00
。
我想要将数字格式化为始终显示两位小数,必要时要四舍五入。
例如:
number display
------ -------
1 1.00
1.341 1.34
1.345 1.35
我一直在使用这个:
parseFloat(num).toFixed(2);
但它显示的是1
,而不是1.00
。
一个更加通用的解决方案,可以将数字舍入到N位小数
function roundN(num,n){
return parseFloat(Math.round(num * Math.pow(10, n)) /Math.pow(10,n)).toFixed(n);
}
console.log(roundN(1,2))
console.log(roundN(1.34,2))
console.log(roundN(1.35,2))
console.log(roundN(1.344,2))
console.log(roundN(1.345,2))
console.log(roundN(1.344,3))
console.log(roundN(1.345,3))
console.log(roundN(1.3444,3))
console.log(roundN(1.3455,3))
Output
1.00
1.34
1.35
1.34
1.35
1.344
1.345
1.344
1.346
roundN(-3.4028230607370965e+38,2)
返回"-3.4028230607370965e+38"
而不是预期的0.00。 - Ferrybig你在寻找地板吗?
var num = 1.42482;
var num2 = 1;
var fnum = Math.floor(num).toFixed(2);
var fnum2 = Math.floor(num2).toFixed(2);
console.log(fnum + " and " + fnum2); //both values will be 1.00
好消息!!! 看起来新版JavaScript,ES2020(我正在使用)提供了此函数的新行为。
let ff:number =3
console.info(ff.toFixed(2)) //3.00
按照要求。
如果你已经在使用jQuery,那么你可以考虑使用jQuery Number Format插件。
该插件可以将格式化的数字作为字符串返回,你可以设置小数点和千位分隔符,并且可以选择要显示的小数位数。
$.number( 123, 2 ); // Returns '123.00'
你也可以从GitHub获取jQuery数字格式化。
这是您所想要的吗?
[编辑20200530] @razu提供的答案我认为是最好的。因此,这里有一个略微重构的版本。
片段代码仍无法返回诸如showAsFloat(2.3346)
这样的值(结果为2.33,但应该是2.34)。因此,请参见也看看。
const showAsFloat = (input, decimals = 2, asString = false) => {
if (input === null || input.constructor === Boolean || isNaN(+input)) {
return input;
}
const converted = +( `${Math.round( parseFloat( `${input}e${decimals}` ) )}e-${decimals}` );
return asString ? converted.toFixed(decimals) : converted
};
document.querySelector('#result').textContent = [
'command | result',
'-----------------------------------------------',
'showAsFloat(1); | ' + showAsFloat(1),
'showAsFloat(1.314); | ' + showAsFloat(1.314),
'showAsFloat(\'notanumber\') | ' + showAsFloat('notanumber'),
'showAsFloat(\'23.44567\', 3) | ' + showAsFloat('23.44567', 3),
'showAsFloat(2456198, 5, true)| ' + showAsFloat('24568', 5, true),
'showAsFloat(2456198, 5) | ' + showAsFloat('24568', 5),
'showAsFloat(0, 2, true); | ' + showAsFloat(0, 2, true),
'showAsFloat(1.345); | ' + showAsFloat(1.345),
'showAsFloat(0.005); | ' + showAsFloat(0.005),
'showAsFloat(null); | ' + showAsFloat(null),
].join('\n');
<pre id="result"></pre>
1.005
四舍五入后为 1.00
,而 1.345
则为 1.34
。 - zcoop98将一个数字转换为字符串,仅保留两位小数:
var num = 5.56789;
var n = num.toFixed(2);
n的结果将会是:
5.57
我刚刚遇到了最长的一条线程之一,以下是我的解决方案:
parseFloat(Math.round((parseFloat(num * 100)).toFixed(2)) / 100 ).toFixed(2)
如果有人能够破解,请告诉我。
function currencyFormat (num) {
return "$" + num.toFixed(2).replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
console.info(currencyFormat(2665)); // $2,665.00
console.info(currencyFormat(102665)); // $102,665.00
在需要特定格式时,您应编写自己的程序或使用库函数来完成所需操作。基本的ECMAScript功能通常不足以显示格式化数字。
关于四舍五入和格式化的详细解释,请参考:http://www.merlyn.demon.co.uk/js-round.htm#RiJ
通常情况下,只有在输出前的最后一步才进行四舍五入和格式化。如果提前进行此操作,可能会引入意外大的误差并破坏格式。
这里有另一种仅使用 floor 函数进行四舍五入的解决方案,确保计算出的金额不会超过原始金额(有时需要用于交易):
Math.floor(num* 100 )/100;