不使用 Math.ceil() javascript 的舍入数
Rounding number without using Math.ceil() javascript
我想将数字四舍五入到最接近的值,但不使用 javascript 中的 Math.ceil() 函数。
通常我们会
Math.ceil(23.99) = 24
但是不使用Math.ceil()怎么实现呢
例如
100.99 -> 101
34.78 -> 35
2.999 -> 3
谁能告诉我如何用简单的 javascript 实现它。不幸的是,我无法在线找到解决方案。
使用toFixed()方法并传递您需要的小数点后位数。请参阅以下示例
console.log("100.99 -> " + (100.99).toFixed(0));
console.log("34.78 -> " + (34.78).toFixed(0));
console.log("2.999 -> " + (2.999).toFixed(0));
我想将数字四舍五入到最接近的值,但不使用 javascript 中的 Math.ceil() 函数。
通常我们会
Math.ceil(23.99) = 24
但是不使用Math.ceil()怎么实现呢 例如
100.99 -> 101
34.78 -> 35
2.999 -> 3
谁能告诉我如何用简单的 javascript 实现它。不幸的是,我无法在线找到解决方案。
使用toFixed()方法并传递您需要的小数点后位数。请参阅以下示例
console.log("100.99 -> " + (100.99).toFixed(0));
console.log("34.78 -> " + (34.78).toFixed(0));
console.log("2.999 -> " + (2.999).toFixed(0));