[英]How to get the ceil value after 2 decimal places using JavaScript
[英]how to get customised ceil and floor value for a given variable?
我想使用javascript自定义ceil和floor。 例如:如果特定的浮点变量10.1,那么我需要将结果作为10(底),如果该值大于10.1,则需要将其设为11(上限)。 这里10是可变的。 谁能给我解决方案?
最好用数学运算,而不要加上小数。 减去整数,检查余数,如果大于.1,则更新整数并将其返回。
function customRound (num) { let wn = Math.floor(num) let adj = num - wn > .1 ? 1 : 0 return wn + adj } console.log('10.1', customRound(10.1)) console.log('10.11', customRound(10.11)) console.log('10.01', customRound(10.1)) console.log('10.100000000001', customRound(10.100000000001))
这是一种方法:
Math.round(thing + 0.4);
假设事物= 2.11,那么您将舍入2.51 =3。如果事物= 2.09,您将舍入2.49 =2。应该可以!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.