JavaScript

How to round decimal numbers in JavaScript

Rounding off is a very common operation when working with numbers in JavaScript. JavaScript provides a Math object that can be used to round decimal numbers. The Math object provides several different methods which can be used to convert decimal numbers into whole numbers. JavaScript also has another method .toFixed() which can also be used to round a decimal number upto or down to certain decimal places.

How to round a decimal number in JavaScript

First we will discuss all the methods provided by the Math object for rounding off a decimal number:

Note: We will use the browser console to demonstrate examples performed in this post.

Math.ceil() method : The Math.ceil() method rounds the decimal number upto its nearest integer:

If we give a negative number as an argument then:

Math.floor() method : This method rounds the decimal number down to its nearest integer:

For a negative number:

Math.round() method : The Math.round() method takes a number as an argument and returns its nearest integer. It rounds the number down to the nearest integer if the digit after the decimal is less than 5:

If the number after the decimal is greater than five then this method rounds the number up to the nearest integer:

If the number after the decimal is 5 then the Math.round() method rounds the number to its nearest integer in the direction of +infinity:

Math.trunc() method : This method truncates the decimal part of a number number and only returns the whole number part:

For negative numbers:

How to use the .toFixed() method : The .toFixed() method is used to round a decimal number upto or down to a specific number of decimals. It takes one optional argument which specifies the number of decimals which by default is 0:

If we want the number to have 2 decimal points then:

If the specified number of decimal points is higher than the decimal points present in the number then 0s are added to create the desired length:

This method converts the number into a string:

Conclusion

Rounding and truncating decimal numbers is a very common problem that every developer has to face. In this post we have listed the five most common solutions present in JavaScript for rounding off or truncating decimal numbers.

The Math.ceil() method rounds the decimal number towards its nearest integer in the direction of +∞ whereas the Math.floor() method rounds the number in the direction of -∞. The Math.round() method rounds the decimal numbers in both directions depending upon the digit that comes after the decimal point. The Math.trunc() method truncates all the digits after the decimal point and returns just the whole number part of the decimal number. The .toFixed() method is used to get the desired number of digits in a decimal number every time.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.