JavaScript

Difference Between Math.round() and Math.trunc() in JavaScript

JavaScript provides several built-in methods such as Math.floor(), Math.ceil(), Math.round(), and Math.trunc. All these methods are used to round off a number; however, each method rounds a number with a different formula/algorithm. For instance, the Math.ceil() method rounds the specific number upward (towards positive infinity) while the floor() method rounds the given number downward(towards negative infinity), etc.

In this write-up we will understand the difference between Math.trunc() and Math.round method. This post will be organized as follows:

  • What is Math.round() and how to use it in JavaScript
  • What is Math.trunc() and how to use it in JavaScript
  • Math.trunc() vs Math.round() in JavaScript

So, let’s begin!

What is Math.round() and how to use it in JavaScript

A built-in method that is used to round off a number to the nearest integer is referred to as the round() method in JavaScript. The below code snippet shows the basic syntax of the Math.round() method:

Math.round(number);

The Math.round() method will take a number as a parameter and round 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 num1= Math.round(10.94);

var num2= Math.round(10.15);

var num3= Math.round(-54.87);

var num4= Math.round(-54.27);

console.log("10.94 rounded to: " , num1);

console.log("10.15 rounded to: ", num2);

console.log("-54.87 rounded to: ", num3);

console.log("-54.27 rounded to: ", num4);

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

From the output we concluded the following points:

  • When the floating-point value is greater than “.5” then the Math.round() method rounds up the number to 15.
  • When the floating-point value is less than “.5” then the Math.round() method rounds down the number to 10.
  • All in all, we can say that the Math.round() method rounds the specified values to the nearest integer values.

What is Math.trunc() and how to use it in JavaScript

It is a predefined math that skips the fractional part and returns only the integer part of the specified value. The below code snippet shows how to use the Math.trunc() method in JavaScript:

Math.trunc(number);

Example

The below code snippet will provide a detailed understanding of how Math.trunc() method works in JavaScript:

var num1= Math.trunc(10.94);

var num2= Math.trunc(10.15);

var num3= Math.trunc(-54.87);

var num4= Math.trunc(-54.27);

console.log("10.94 rounded to: " , num1);

console.log("10.15 rounded to: ", num2);

console.log("-54.87 rounded to: ", num3);

console.log("-54.27 rounded to: ", num4);

The Math.trunc() method will remove the floating points and it will generate the following output:

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

Math.trunc() vs Math.round() in JavaScript

As of now, we have seen how Math.trunc() and Math.round() methods work in JavaScript. Let’s conclude what we have learned from the above examples:

  • The round() method rounds the number on the basis of fractional/floating point value i.e. if the fractional value is greater than “.50” then the round() method will round the number upward(towards positive infinity). If the fractional value is less than “.50” then the round() method will round the number downward (towards negative infinity).
  • For example, the round() method will return 26 if the value is 25.50 while it will return 25 if the value is 25.49.
  • Now if we talk about the Math.trunc() method it skips the fractional part regardless of the fractional value i.e., it doesn’t matter what comes after the decimal point either its greater than “.50” or less than “.50” the trunc method will skip the floating-point part.
  • For example, the trunc() method will return 25 in both cases i.e. either the value is 25.50 or its 25.49.

Conclusion

Math.round() and Math.trunc() are two predefined methods in JavaScript that are used to round a number. The difference between both these methods is that the Math.trunc() method cut-off the fractional part and returns the remaining integer value, however the Math.round() method rounds the number to the nearest integer. In this write-up we have learned the key difference between Math.round() and Math.trunc() method with the help of appropriate examples.

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.