Home > Net >  Rounding number without using Math.ceil() javascript
Rounding number without using Math.ceil() javascript

Time:12-15

I want to round a number to nearest value but without using a Math.ceil() function in javascript.

usually we do

Math.ceil(23.99) = 24

But how can we achieve it without using Math.ceil() for e.g.

100.99 -> 101
34.78 -> 35
2.999 -> 3

Can someone please let me know how to achieve it with plain javascript. Unfortunately, i could not find a solution online.

CodePudding user response:

Use toFixed() method and pass the number of digits that you need after decimal. See below examples

console.log("100.99 -> "   (100.99).toFixed(0));
console.log("34.78 -> "   (34.78).toFixed(0));
console.log("2.999 -> "   (2.999).toFixed(0));

  • Related