How to round to at most two decimal places in JavaScript

# How to round to at most two decimal places in JavaScript

Matthew C.

You want to round a number to two decimal places, at most. How do you do this?

## The Solution

The `Math.round()` method rounds a number to the nearest integer. You can use it along with some arithmetic to round a number to a specific number of decimal places. To round a number to two decimal places at most, multiply the number by 10 to the power of 2. This moves the decimal place to the position where you want rounding to occur. Then round the number to the nearest integer using `Math.round()` and divide the answer by 10 to the power of 2:

Click to Copy
```.css-3i4pd0{position:relative;color:inherit;-webkit-transition:box-shadow 0.3s;transition:box-shadow 0.3s;will-change:box-shadow;outline:0;display:-webkit-box;display:-webkit-flex;display:-ms-flexbox;display:flex;-webkit-flex-direction:column;-ms-flex-direction:column;flex-direction:column;z-index:9;}.css-3i4pd0:hover,.css-3i4pd0:focus{cursor:pointer;}.css-3i4pd0:hover .e1fliqfg1,.css-3i4pd0:focus .e1fliqfg1{fill:#e1567c;cursor:pointer;}const num = 1.151;
console.log(Math.round(num * 10 ** 2) / 10 ** 2); // 1.15
```

You may occasionally get rounding errors using this method. For example, if you round `1.005` to two decimal places, you would expect to get an answer of `1.01`:

Click to Copy
```const num = 1.005;
console.log(Math.round(num * 10 ** 2) / 10 ** 2); // 1
```

However, the answer that you would get is `1`. This is because of floating point math and because a computer stores data in binary format. This causes the number `1.005` to be stored as `1.004999999888241291046142578125`, which causes the number to be rounded down to `1`.

Note that there is a limit to the precision with which a number with decimals can be stored. The ECMAScript standard defines a set size limit for the JavaScript `Number` type.

This rounding problem can be fixed using exponential notation:

Click to Copy
```const num = 1.005;
console.log(Number(`\${Math.round(`\${num}e2`)}e-2`)); // 1.01
```

If the number is already in exponential notation, you’ll need to convert it to a `Number` first. You can do this using the `toFixed()` method:

Click to Copy
```const num = (1.0449433327097e5).toFixed(20);
```

The popular Lodash utility library uses this exponential notation trick for its `createRound()` function.

If you are rounding negative numbers, be careful to note that `-0.5` rounds to `-0` and not `-1`. If the fractional portion of a number is exactly `0.5`, `Math.round` rounds the number to the next integer in the direction of +∞ (positive infinity).

• ResourcesImprove Web Browser Performance - Find the JavaScript code causing slowdowns
• ResourcesJavaScript Frontend Error Monitoring 101
• Listen to the Syntax Podcast

Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.

SEE EPISODES

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

### Platforms

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.