在 javascript 中四舍五入小数点
round off decimal points in javascript
我正在尝试四舍五入小数点。以下是场景
Entered Value - 56.6
Round off Value - 57
Entered Value - 56.7
Round off Value - 57
Entered Value - 56.8
Round off Value - 57
但是当我尝试舍入 56.3/56.2/56.4
时,同样的事情正在显示相同的值。
当我尝试从 56.1
到 56.4
时,我得到了相同的值。但是当我尝试从 56.5
到 56.9
值四舍五入到 57
Entered Value - 56.3
Round off Value - 56.3
Expected Value - 57
我试过下面的代码来轮
parseFloat(value).toFixed();
Math.round(value)
如何真正执行到四舍五入的所有场景?
Math.round()
四舍五入到最接近的整数。因此它向下舍入 .5
以下的分数,向上舍入 .5
.
以上的分数
如果你总是想向上取整,使用Math.ceil()
。
value = 56.3;
console.log(Math.ceil(value));
我正在尝试四舍五入小数点。以下是场景
Entered Value - 56.6
Round off Value - 57
Entered Value - 56.7
Round off Value - 57
Entered Value - 56.8
Round off Value - 57
但是当我尝试舍入 56.3/56.2/56.4
时,同样的事情正在显示相同的值。
当我尝试从 56.1
到 56.4
时,我得到了相同的值。但是当我尝试从 56.5
到 56.9
值四舍五入到 57
Entered Value - 56.3
Round off Value - 56.3
Expected Value - 57
我试过下面的代码来轮
parseFloat(value).toFixed();
Math.round(value)
如何真正执行到四舍五入的所有场景?
Math.round()
四舍五入到最接近的整数。因此它向下舍入 .5
以下的分数,向上舍入 .5
.
如果你总是想向上取整,使用Math.ceil()
。
value = 56.3;
console.log(Math.ceil(value));