如何解决如何舍入至多 2 位小数?
使用Math.round()
:
Math.round(num * 100) / 100
或者更具体地说,为了确保 1.005 正确舍入,请使用Number.EPSILON:
Math.round((num + Number.EPSILON) * 100) / 100
如果值是文本类型:
parseFloat("123.456").toFixed(2);
如果值是一个数字:
var numb = 123.23454;
numb = numb.toFixed(2);
有一个缺点是,像 1.5 这样的值会给出“1.50”作为输出。@minitech 建议的修复:
var numb = 1.5;
numb = +numb.toFixed(2);
// Note the plus sign that drops any "extra" zeroes at the end.
// It changes the result (which is a string) into a number again (think "0 + foo"),
// which means that it uses only as many digits as necessary.
这似乎Math.round
是一个更好的解决方案。在某些情况下,它正确舍入:
Math.round(1.005 * 100)/100 // Returns 1 instead of expected 1.01!
在某些情况下, toFixed() 也正确舍入(在 Chrome v.55.0.2883.87 中测试)!
例子:
parseFloat("1.555").toFixed(2); // Returns 1.55 instead of 1.56.
parseFloat("1.5550").toFixed(2); // Returns 1.55 instead of 1.56.
// However, it will return correct result if you round 1.5551.
parseFloat("1.5551").toFixed(2); // Returns 1.56 as expected.
1.3555.toFixed(3) // Returns 1.355 instead of expected 1.356.
// However, it will return correct result if you round 1.35551.
1.35551.toFixed(2); // Returns 1.36 as expected.
我猜,这是因为 1.555 实际上在幕后类似于 float 1.55499994。
是使用具有所需舍入算法的脚本,例如:
function roundNumber(num, scale) {
if(!("" + num).includes("e")) {
return +(Math.round(num + "e+" + scale) + "e-" + scale);
} else {
var arr = ("" + num).split("e");
var sig = ""
if(+arr[1] + scale > 0) {
sig = "+";
}
return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
}
}
这不是每个人的通用解决方案。有几种不同的舍入算法,您的实现可能会有所不同,具体取决于您的要求。
是避免前端计算并从后端服务器拉取舍入值。
另一种可能的解决方案,这也不是防弹的。
Math.round((num + Number.EPSILON) * 100) / 100
在某些情况下,当你对 1.3549999999999998 这样的数字进行四舍五入时,它会返回不正确的结果。应该是 1.35,但结果是 1.36。
解决方法
我想最多四舍五入小数点后 2 位,但仅限于必要时。
输入:
10
1.7777777
9.1
输出:
10
1.78
9.1
如何在 JavaScript 中做到这一点?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。