DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

How to Round Numbers in JavaScript

Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62

Subscribe to my email list now at http://jauyeung.net/subscribe/

JavaScript has multiple ways to round a number. Some choices are the Math.round , number.toFixed , andnumber.toPrecision . You can also write your own function to round a number up or down to the nearest increment.

Math.round

Math.round rounds a number to the nearest integer. If the decimal part of the number is less than 0.5, it is rounded down. Otherwise, if the decimal part of the number of 0.5 or higher then it will be rounded up. The function returns the rounded number as the value.

For example:

Math.round(12.5); // 13  
Math.round(12.49); // 12
Enter fullscreen mode Exit fullscreen mode

Number.toFixed

You can set the number of digits that appears after the decimal place with toFixed function. The function returns the string representation of the number as the value. It can be used like this:

const a = 12.8888888888;  
const b = a.toFixed(2); // 12.88
Enter fullscreen mode Exit fullscreen mode

Number.toPrecision

Number.toPrecision is similar to toFixed . It returns the string representation of a number, but you can round it to the specified number of significant digits which you can specify or let it automatically round to the correct number of significant digits.

const a = 12.8888888888;  
const b = a.toPrecision(2); // 13, since 2 significant digits is specified  
const c = a.toPrecision(); // 12.8888888888, since all digits are significant in the original number
Enter fullscreen mode Exit fullscreen mode

Round to the nearest Increment

You can round to the nearest increment up or down you specify:

const roundNumberUp = (num, increment) => {   
  return Math.ceil(num / increment) \* increment;  
}  
console.log(roundNumberUp(12.2, 0.5)) // 12.5
Enter fullscreen mode Exit fullscreen mode

What this does is take the original number, divide by the increment you want to round up to, then take the ceiling of that, then multiply by the increment. This means the number should always round up.

Similarly, you can round down to the nearest increment with floor.

const roundNumberUp = (num, increment) => {   
  return Math.floor(num / increment) \* increment;  
}  
console.log(roundNumberUp(12.2, 0.5)) // 12.5
Enter fullscreen mode Exit fullscreen mode

What this does is take the original number, divide by the increment you want to round up to, then take the floor of that, then multiply by the increment. This means the number should always round down.

Top comments (3)

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

A very quick way of doing something VERY similar to Math.floor is just ~~

~~2.4  // 2
~~10.9 // 10
Enter fullscreen mode Exit fullscreen mode

Be careful with this though - all it does is remove the decimal part. It doesn't behave in the same way as Math.floor with negative numbers

Collapse
 
nikhilmwarrier profile image
nikhilmwarrier

Awesome read! What about Math.floor()???

Collapse
 
aumayeung profile image
John Au-Yeung

Thanks. I think I have that in another article.