Use Math.round() :

Math.round(num * 100) / 100

Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :

Math.round((num + Number.EPSILON) * 100) / 100
Answer from Brian Ustas on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › round
Math.round() - JavaScript | MDN
July 10, 2025 - The Math.round() static method returns the value of a number rounded to the nearest integer.
🌐
W3Schools
w3schools.com › jsref › jsref_round.asp
JavaScript Math round() Method
The Math.round() method rounds a number to the nearest integer.
Discussions

Math formula to round numbers to ceiling
HI, so i need to round numbers up to the nearest full integer. So for instance if my number is 1.2 it has to round up to 2. But i noticed that with the “round” formula it rounds down to the nearest integer which would be 1. But in my use case this would lead to wrong calculations. More on community.weweb.io
🌐 community.weweb.io
0
March 11, 2023
floating point - How do I round a number in JavaScript? - Stack Overflow
What I want to output are pure integers. I don't know a lot of JavaScript, though. How would I go about writing a method that takes these sometimes-floats and makes them integers? ... I'd also recommend calling Math.floor() before so it doesn't round up if that's what you're looking for. More on stackoverflow.com
🌐 stackoverflow.com
Why not always use Math.round instead of Math.floor?

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

More on reddit.com
🌐 r/javascript
13
1
February 22, 2017
how to round to 2 decimal places with typescript?
It's not a typescript question, it's a javascript question (maybe even just a math question). But what you are looking for is the toFixed() method. (5).toFixed(2) // returns the string "5.00" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed btw : your first example "123.45 => should return 123.46" there should be no rounding here it should return "123.45", and toFixed() will take care of rounding for you. More on reddit.com
🌐 r/typescript
9
1
December 25, 2019
Top answer
1 of 16
5568

Use Math.round() :

Math.round(num * 100) / 100

Or to be more specific and to ensure things like 1.005 round correctly, use Number.EPSILON :

Math.round((num + Number.EPSILON) * 100) / 100
2 of 16
4380

If the value is a text type:

parseFloat("123.456").toFixed(2);

If the value is a number:

var numb = 123.23454;
numb = numb.toFixed(2);

There is a downside that values like 1.5 will give "1.50" as the output. A fix suggested by @minitech:

var numb = 1.5;
numb = +numb.toFixed(2);
// Note the plus sign that drops any "extra" zeroes at the end.
// It changes the result (which is a string) into a number again (think "0 + foo"),
// which means that it uses only as many digits as necessary.

It seems like Math.round is a better solution. But it is not! In some cases it will not round correctly:

Math.round(1.005 * 100)/100 // Returns 1 instead of expected 1.01!

toFixed() will also not round correctly in some cases (tested in Chrome v.55.0.2883.87)!

Examples:

parseFloat("1.555").toFixed(2); // Returns 1.55 instead of 1.56.
parseFloat("1.5550").toFixed(2); // Returns 1.55 instead of 1.56.
// However, it will return correct result if you round 1.5551.
parseFloat("1.5551").toFixed(2); // Returns 1.56 as expected.

1.3555.toFixed(3) // Returns 1.355 instead of expected 1.356.
// However, it will return correct result if you round 1.35551.
1.35551.toFixed(2); // Returns 1.36 as expected.

I guess, this is because 1.555 is actually something like float 1.55499994 behind the scenes.

Solution 1 is to use a script with required rounding algorithm, for example:

function roundNumber(num, scale) {
  if(!("" + num).includes("e")) {
    return +(Math.round(num + "e+" + scale)  + "e-" + scale);
  } else {
    var arr = ("" + num).split("e");
    var sig = ""
    if(+arr[1] + scale > 0) {
      sig = "+";
    }
    return +(Math.round(+arr[0] + "e" + sig + (+arr[1] + scale)) + "e-" + scale);
  }
}

It is also at Plunker.

Note: This is not a universal solution for everyone. There are several different rounding algorithms. Your implementation can be different, and it depends on your requirements. See also Rounding.

Solution 2 is to avoid front end calculations and pull rounded values from the backend server.

Another possible solution, which is not a bulletproof either.

Math.round((num + Number.EPSILON) * 100) / 100

In some cases, when you round a number like 1.3549999999999998, it will return an incorrect result. It should be 1.35, but the result is 1.36.

🌐
Medium
medium.com › @python-javascript-php-html-css › javascript-rounding-numbers-to-a-maximum-of-two-decimal-places-775b9650d6fb
JavaScript Rounding Numbers to a Maximum of Two Decimal Places
September 29, 2024 - Yes, the same rounding functions can be used in both frontend and backend JavaScript environments. ... You can use the Math.round() function to round a number to the nearest integer.
🌐
WeWeb Community
community.weweb.io › ask us anything › how do i?
Math formula to round numbers to ceiling - How do I? - WeWeb Community
March 11, 2023 - HI, so i need to round numbers up to the nearest full integer. So for instance if my number is 1.2 it has to round up to 2. But i noticed that with the “round” formula it rounds down to the nearest integer which would be…
🌐
Math.js
mathjs.org › docs › reference › functions › round.html
math.js | an extensive math library for JavaScript and Node.js
Round a value towards the nearest rounded value. For matrices, the function is evaluated element wise. math.round(x) math.round(x, n) math.round(unit, valuelessUnit) math.round(unit, n, valuelessUnit) Type | Description —- | ———– ·
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › article › How-to-round-up-a-number-in-JavaScript
How to round up a number in JavaScript?
November 7, 2022 - In the above example, we have applied the Math.floor() property to three different variables x, y, and, z. First, we create the variable ?x? it is an integer so when we apply the Math.floor property to it and assign that value to variable ?value1? ...
🌐
Can I Use
caniuse.com › mdn-javascript_builtins_math_round
JavaScript built-in: Math: round | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
🌐
CoreUI
coreui.io › answers › how-to-round-a-number-in-javascript
How to round a number in JavaScript · CoreUI
September 26, 2025 - The Math.round() function rounds a number to the nearest integer using standard mathematical rounding (0.5 rounds up). In the first example, Math.round(3.7456) returns 4 because 3.7456 is closer to 4 than to 3. For decimal precision, multiply ...
🌐
JavaScripter
javascripter.net › faq › rounding.htm
Rounding in JavaScript
Math.round(X); // round X to an integer Math.round(10*X)/10; // round X to tenths Math.round(100*X)/100; // round X to hundredths Math.round(1000*X)/1000; // round X to thousandths ...
🌐
Code.mu
code.mu › en › javascript › manual › math › Math.round
The Math.round method - a number rounding in JavaScript
The round method of the Math object rounds to the nearest whole number using the rules of mathematical rounding in JavaScript.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › round() in javascript
round() in JavaScript | Examples of round() in JavaScript
March 2, 2023 - This function is used to round off the number to the nearest integer. The concept of rounding off the number is if the fractional part of the number is greater than or equal to 0.5, then the number will be rounded off to the next higher integer.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › round
JavaScript Math round() - Round Number to Integer | Vultr Docs
December 2, 2024 - The Math.round() function in JavaScript is a method used to round a number to the nearest integer.
🌐
Reddit
reddit.com › r/javascript › why not always use math.round instead of math.floor?
r/javascript on Reddit: Why not always use Math.round instead of Math.floor?
February 22, 2017 -

When I read through the code of colleagues and public repos, I see Math.floor used like 20x more often than Math.round.

But why? Isn't Math.round more accurate than Math.floor? Shouldn't it be the other way around (using Math.round more often than Math.floor)?

Is Math.floor so much faster than Math.round or am I missing something?

Edit

I am aware that those two do different things. My point is that in my experience, Math.floor is much too often used, when Math.round would simply be more accurate.

Top answer
1 of 6
9

Well, they are two different functions, with two different uses. Math.floor() always rounds down to the nearest integer, while Math.round() will round up or down depending on what side of .5 the number falls on. So, the basic answer is that you use which one gets the result you expect.

When it comes to generating random numbers though, Math.floor() has a more even distribution than Math.round(). If you want to generate a random number between 0 and 2, take the following examples:

Math.floor(Math.random() * 3). Here, 0-0.999999 will give you 0, 1.0 to 1.999999 will give you 1, and 2.0 to 2.999999 will give you 2. Every number has a 33% chance of being the result.

Math.round(Math.random() * 2). Here, 0-0.499999 will give you 0, 0.5 to 1.499999 will give you 1, and 1.5 to 1.999999 will give you 2. Note that the range of numbers that lead to a 1 is twice as big as those that lead to 0 or 1. That is 25% chance of 0, 50% chance of 1, and 25% chance of 2.

2 of 6
3

Math.floor - You have a rating system of stars, and you aren't breaking them up into half stars. You do a query to get all the votes and the math comes back to 4.7 stars. You would use Math.floor here so that you display 4 stars.

 

Math.ceil - You have a slider module that displays 3 slides at a time. This module contains 19 slides. 19/3 = 6.33. If you were to floor or round here you would end up with 6. But to make sure that all 19 slides are shown, you need 7 containers, so you use Math.ceil.

 

Math.round - Anytime you need the closest number without worrying about anything like the above scenarios.

🌐
SheCodes
shecodes.io › athena › 109834-how-to-use-math-round-in-a-javascript-function
[JavaScript] - How to use Math.round() in a JavaScript | SheCodes
Learn how to use the Math.round() function in JavaScript to round a number to the nearest whole number in a function.
🌐
SitePoint
sitepoint.com › blog › javascript › a guide to rounding numbers in javascript
A Guide to Rounding Numbers in JavaScript — SitePoint
November 13, 2024 - We can use Math.fround to see how it’s actually represented: ... As you can see, it’s actually represented by the floating point number 3.549999952316284, which rounds down to 3.5. These problems with rounding numbers in JavaScript don’t occur too often, but they’re definitely something you should be aware of if you’re doing a lot of rounding — especially when it’s important that the result is accurate.
🌐
Robin Wieruch
robinwieruch.de › javascript-rounding-errors
JavaScript Rounding Errors (in Financial Applications) - Robin Wieruch
June 24, 2024 - The Math.round() function rounds a number to the nearest integer which you usually want when working with financial applications where you want to keep monetary values as integers (read: cents).
🌐
Way2tutorial
way2tutorial.com › javascript › example › round_function.php
JavaScript Math.Round() function
JavaScript round() function return the nearest integer value of rounded value. ... <!DOCTYPE html> <html lang="en"> <head> <title>JavaScript Math.round function</title> </head> <body> <script type="text/javascript"> document.write(Math.round(4.60) + "<br />"); document.write(Math.round(8.32) ...