JavaScript

Different ways to round numbers in JavaScript

In JavaScript, Math is a predefined object that holds various properties and methods. It can be used to perform different mathematical functionalities on the numbers. In JavaScript, Math is a static object hence we can invoke all the methods or properties of Math directly (i.e., without creating the object). JavaScript offers different ways/functions that can be used with the Math object to round off a number.This write-up will explain the below listed concepts that will assist you to understand how to round off the numbers in JavaScript:

So, let’s begin!

What is Math.round() in JavaScript

It is a predefined method in JavaScript and is used with the Math object to round off a number to the nearest integer. The below snippet depicts the basic syntax of the Math.round() method:

Math.round(number);

The Math.round() method takes a value and rounds the specified value to the nearest integer.

Example

In this example, we will consider various scenarios to understand how Math.round() method works in JavaScript:

var number1= Math.round(14.49);

var number2= Math.round(14.50);

var number3= Math.round(14.87);

var number4= Math.round(14.27);

console.log("14.49 rounded to: " , number1);

console.log("14.50 rounded to: ", number2);

console.log("14.87 rounded to: ", number3);

console.log("14.27 rounded to: ", number4);

In the above snippet, we passed four different values(one-by-one) to the Math.round() method and printed them on the browser’s console:

From the output we observed that when the floating-point value is less than “.5” then the Math.round() method rounded down the number to 14. If the floating-point value is greater than “.5” then the Math.round() method rounded up the number to 15. It authenticates that the round() method rounds the specified values to the nearest integer values.

What is Math.ceil() in JavaScript

It is a predefined method in JavaScript that rounds a number upward to the nearest integer number. The below snippet depicts the basic syntax of the Math.ceil() method:

Math.ceil(number);

Example

In this example, we will understand the working of Math.ceil() method in JavaScript:

var number1= Math.ceil(14.49);

var number2= Math.ceil(14.50);

var number3= Math.ceil(14.87);

var number4= Math.ceil(14.27);

console.log("14.49 rounded to: " , number1);

console.log("14.50 rounded to: ", number2);

console.log("14.87 rounded to: ", number3);

console.log("14.27 rounded to: ", number4);

In this example, we considered exactly the same values as of example1, the only difference is that this time we utilized the Math.ceil() method instead of Math.round(). As a result, we will get the following output:

From the output, we come to know that the ceil() method rounded the specified value upward to the nearest integer value.

What is Math.floor() in JavaScript

It is a predefined method in JavaScript that rounds a number downward to the nearest integer number. The below snippet shows the basic syntax of the Math.floor() method:

Math.floor(number);

Example3

The below snippet will explain the working of Math.floor() method in JavaScript:

var number1= Math.floor(14.49);

var number2= Math.floor(14.50);

var number3= Math.floor(14.87);

var number4= Math.floor(14.27);

console.log("14.49 rounded to: " , number1);

console.log("14.50 rounded to: ", number2);

console.log("14.87 rounded to: ", number3);

console.log("14.27 rounded to: ", number4);

The Math.floor() method will generate the following output:

The output shows that the Math.floor() method rounded the specified value downward to the nearest integer value.

What is Math.trunc() in JavaScript

It is a predefined method that skips the floating-point/fractional values and returns only the integer part of the specified value. The below snippet shows the basic syntax of the Math.trunc() method:

Math.trunc(number);

Example

The below snippet shows how Math.trunc() method works in JavaScript:

var number1= Math.trunc(14.49);

var number2= Math.trunc(14.50);

var number3= Math.trunc(14.87);

var number4= Math.trunc(14.27);

console.log("14.49 rounded to: " , number1);

console.log("14.50 rounded to: ", number2);

console.log("14.87 rounded to: ", number3);

console.log("14.27 rounded to: ", number4);

The Math.trunc() method will remove the fractional part and the output will be as follows:

The output verifies the working of Math.trunc() method in JavaScript.

Conclusion

In JavaScript, Math.round(), Math.trunc(), Math.ceil(), and Math.floor() methods are used to round the numbers. The Math.round() method rounds the number to the nearest integer, Math.floor() method rounds the number downward (i.e., towards the negative infinity), the Math.ceil() method rounds the number upward (i.e., towards the positive infinity), and the Math.trunc() method keeps the integer value and skips the fractional part of the given value. This write-up explained different ways to round the numbers in JavaScript.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.