# Round to the nearest hundredth of a decimal in JavaScript

## ðŸ“‹ Table Of Content

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:

- Math.round()
- 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.

**Example:**

```
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.

**Syntax:**

```
number.toFixed(x)
```

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.

### Recent Post

- Check for prime number using for and while loop in Python
- Merge two dictionaries together using python (3 ways)
- Merge multiple JSON objects into one single object in Python
- Python - Check if list is empty or not
- What is memoization and how to cache function results in Javascript
- Python - Insert an element at specific index in a list