Round to the nearest hundredth of a decimal in JavaScript

In this short article, we will learn how to round to the nearest hundredth of a decimal in JavaScript.

Suppose we have a decimal number 1.87999999997 and we have to round it to 2 decimals so that we get 1.88 as the return value.

To do this we can use two methods in JavaScript:

  1. Math.round()
  2. toFixed()

So, let's see how to use these two methods with examples to round it to the nearest hundredth of a decimal.

Method 1 : Using Math.round()

The Math.round() method is used to return a value of a number that is rounded to the nearest integer.

console.log(Math.round(2.6898)) // 3

Since Math.round() returns only the nearest integer, in order to get the nearest hundredth of a decimal of a given number, we can follow the steps below.

First, multiply the number by 100 and then divide it by the same value ( here, in this case, it is 100, as we are rounding it off to the nearest hundredth of the decimal). This will give us the resultant rounded off to two decimal places.


function roundNumber(num) {
    return Math.round(num * 100) / 100;

console.log(roundNumber(1.87999999997)) // 1.88

Method 2 : Using toFixed()

The toFixed() method in Javascript converts a number to a string. This method also helps us to round off the string to a specific number of decimals.



x: It specifies the number of decimals we need.

Now, let's code a function that can round off to the nearest hundredth of the decimal using toFixed().

function roundToHundredth(value){
  return Number(value.toFixed(2));

console.log(roundToHundredth(1.8699996666)) // 1.87
console.log(roundToHundredth(2.14259)) // 2.14
console.log(roundToHundredth(3.67628)) // 3.68

Since toFixed() only returns a string so we have used the Number() to convert it back to the data type number.

💾 Recommended Articles