Math.floor() 在 javascript 中四舍五入到最接近的 0.5 [英] Math.floor () to round down to nearest 0.5 in javascript
本文介绍了Math.floor() 在 javascript 中四舍五入到最接近的 0.5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个我使用的 excel 函数:=FLOOR(value,0.5)
I have a function from excel that I use: =FLOOR(value,0.5)
使用此函数,我将向下舍入到最接近的一半值.例如:
With this function, I round DOWN to the nearest half value. For example:
10.55 变成 10.510.99 变成 10.510.2 变成 10
10.55 becomes 10.5 10.99 becomes 10.5 10.2 becomes 10
等等.
javascript 中是否有等价物?我知道 Math.floor() 会四舍五入到最接近的整数.但是,有没有人知道将四舍五入到最接近的 0.5 的好方法?
Is there an equivalent in javascript? I know that Math.floor () will round down to the nearest integer. However, does anyone know of a good way to round down to the nearest 0.5 instead?
先谢谢你
推荐答案
这是你的功能:
function slipFloor(num){
let f = Math.floor(num);
if(num-f < 0.5){
return f;
}
return f+0.5;
}
console.log(slipFloor(10.55));
console.log(slipFloor(10.99));
console.log(slipFloor(10.2));
这篇关于Math.floor() 在 javascript 中四舍五入到最接近的 0.5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文