即使在 javascript 无限位数中,如何在点后做半圆? [英] how to do round half even in javascript unlimited digits after point?
问题描述
我想将 26.955 四舍五入为 26.96
I want to have 26.955 rounded to 26.96
我的意思是如果我有数字 0-4 我想把它们四舍五入如果他们是 5-9 我想把他们围起来
I mean if I have numbers 0-4 I want to round them down and if they are 5-9 I want to round them up
我考虑过 parseFloat(number).toFixed(2)
但是它返回给我 26.95,我需要 26.96我使用了 Math.round
但这也不起作用,我看到了 Math.round10
但它告诉我这个函数不存在所以我不知道如何解决我的问题.
I considered parseFloat(number).toFixed(2)
however it returns me 26.95, I need 26.96
I used Math.round
but this doesn't work either, I saw Math.round10
but it told me this function doesn't exist so I don't know how to solve my problem.
更新:在点之后我并不总是有 3 位数字,我有超过 26.956736489
UPDATE: I don't have always 3 digits after point I have more than that I would have 26.956736489
你提到的重复谈论 .fixed(2) 我是说它甚至不能工作一半它不是重复
your mentioned duplicate talks about .fixed(2) I am saying it is not working for half even it is not a duplicate
推荐答案
尝试
Math.round(29.955*100)/100; //29.96
或者很酷的函数式方法
function round(n) {
n = Math.pow(10,n);
return function(num) {
return Math.round(num*n)/n;
}
}
var round2 = round(2);
var num = 29.9555;
console.log(round2(num));
这篇关于即使在 javascript 无限位数中,如何在点后做半圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!