JavaScript

Different Methods to Round Float Numbers in JavaScript?

Rounding numbers is an important and useful task in the JavaScript programming language in multiple cases. For instance, programmers might want to display numbers with a limited number of decimal places or whole numbers to make them easier to read or to fit them into a small space.

This tutorial will illustrate the methods to round the floating/decimal numbers in JavaScript.

Different Methods to Round the Float/Decimal Numbers in JavaScript?

In JavaScript, the below given pre-built methods are used to round the float numbers:

Method 1: Round the Float Numbers Using the “Math.round()” Method

Use the “Math.round()” method to round the floating or decimal numbers. It will round the given number to the closest whole number/integer. If the decimal portion/part of the specified number is 0.5 or larger, the number is rounded up. Otherwise, it is rounded down.

Syntax
Use the given syntax for the Math.round() method:

Math.round(floatNumber);

Example
Pass the decimal number “25.5911” to the round() method as an argument:

Math.round(25.5911);

It can be seen that the output displays the “26”, which is the closest whole number, and also it rounds up because of the “0.5911”, which is the decimal part of the number:

Method 2: Round the Float Numbers Using the Math.floor() Method

For rounding the float numbers use the “Math.Floor()” method. It takes a number and rounds it down to the nearest or closest integer. It always rounds down, even if the decimal part of the number is 0.5 or greater than 0.5.

Syntax
Follow the given syntax for the Math.floor() method:

Math.floor(floatNumber);

Example
Call the floor() method of the Math object and pass the decimal number to round it:

Math.floor(25.5911);

Output

Method 3: Round the Float Numbers Using the Math.ceil() Method

To round the floating number, use the “Math.ceil()” method. It accepts a decimal number and rounds it up to the nearest or closest integer. It always rounds up, even if the decimal part of the number is less than 0.5.

Syntax
The following syntax is utilized for the Math.ceil() method:

Math.ceil(floatNumber);

Example
Invoke the Math.ceil() method and pass the decimal number “25.5911” as an argument:

Math.ceil(25.5911);

It gives the closest integer number by rounding up it as an output because of the number’s decimal part “0.5911”:

Method 4: Round the Float Numbers Using the Math.trunc() Method

Another method to round the float number is the “Math.trunc()” method. It is used to truncate the decimal portion of a number, effectively removing the fractional part and returning only the integer portion of the number.

Syntax
The below-mentioned syntax is utilized for the Math.trunc() method:

Math.trunc(floatNumber);

Example
Pass the decimal number to the trunc() method to truncate the decimal part of the number:

Math.trunc(25.5911);

Method 5: Round the Float Numbers Using the toFixed() Method

If you want to round the float number to a certain number of decimal points, utilize the “toFixed()” method. It is used to specify the number of decimal places to utilize when formatting a number as a string.

Syntax
For rounding numbers on the specified number of decimal places, use the given syntax:

floatNumber.toFixed()

Example
Round the number “25.5911” to the 2 decimal places:

console.log(25.5911.toFixed(2));

It can be seen that the given output displays the float number up to 2 decimal places:

Call the toFixed() method with a number to round the number up to 1 decimal place:

console.log(25.5911.toFixed(1));

Output

That’s all about the rounding float numbers in JavaScript.

Conclusion

To round the floating numbers in JavaScript, use the “Math.round()”, “Math.floor()”, “Math.ceil()”, “Math.trunc()” or the “toFixed()” method. The round() method rounds the number up to the closest whole number, floor() method rounds down to the closest integer, ceil() method rounds up to the nearest or closest integer. trunc() method truncates the decimal portion of a number, while the toFixed() method is used to format a number to a certain number of decimal points. This tutorial illustrated the methods to round the floating-point numbers in JavaScript with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.