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
🌐
W3Schools
w3schools.com › jsreF › jsref_round.asp
W3Schools.com
The Math.round() method rounds a number to the nearest integer.
Discussions

How to round to at most 2 decimal places, if necessary
Performance should be a concern also, which could make this approach less desirable. Math.round() is much faster. jsbin.com/kikocecemu/edit?js,output 2021-09-28T16:51:12.21Z+00:00 More on stackoverflow.com
🌐 stackoverflow.com
JavaScript math, round to two decimal places - Stack Overflow
I'd just add that for the specific ... = Math.round(10000 * (1 - price / listprice)) / 100; @ChristopheRoussy, the example you provided is actually correct and this result is caused by the fact that base-2 floating point can't represent all base-10 decimals precisely. In this case 1.005 * 100 in JS is actually ... 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
January 8, 2016
Math rounding function
The first function will return the average sum, in the case of this array 4.125 but by a decimal place. Any ideas how i can implement the second function to bring the number to a whole number · The round function you wrote doesn’t even use its argument so won’t do anything really, unless ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
0
April 14, 2018
🌐
Math.js
mathjs.org › docs › reference › functions › round.html
math.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 —- | ———– ·
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 › swlh › how-to-round-to-a-certain-number-of-decimal-places-in-javascript-ed74c471c1b8
How to Round to a Certain Number of Decimal Places in JavaScript | by Dr. Derek Austin 🥳 | The Startup | Medium
January 5, 2023 - In this article, you will learn how to use Math.round() to round a JavaScript number to a certain number of decimal places with a little arithmetic.
🌐
SitePoint
sitepoint.com › blog › javascript › a guide to rounding numbers in javascript
A Guide to Rounding Numbers in JavaScript — SitePoint
November 13, 2024 - Yes, Math.round() works for both positive and negative numbers. It rounds positive numbers as usual and rounds negative numbers towards zero. ... Yes, due to the way floating-point arithmetic works in computers, rounding errors can occur.
Find elsewhere
🌐
Medium
jessijokes.medium.com › the-simple-and-not-boring-guide-to-rounding-accurately-in-javascript-98748810fccf
The Simple and Not Boring Guide to Rounding Accurately in Javascript | by Jessica Bradham | Medium
November 16, 2023 - So instead of (1.005 * 100) / 100 we’re going to be doing (e3 and e-3 if you want it rounding to the thousands place, e4 and e-4 for ten thousands, etc.
🌐
npm
npmjs.com › package › mathjs
mathjs - npm
1 month ago - Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to ...
      » npm install mathjs
    
Published   Feb 10, 2026
Version   15.1.1
Author   Jos de Jong
Homepage   https://mathjs.org
Top answer
1 of 12
1031

NOTE - See Edit 4 if 3 digit precision is important

var discount = (price / listprice).toFixed(2);

toFixed will round up or down for you depending on the values beyond 2 decimals.

Example: http://jsfiddle.net/calder12/tv9HY/

Documentation: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed

Edit - As mentioned by others this converts the result to a string. To avoid this:

var discount = +((price / listprice).toFixed(2));

Edit 2- As also mentioned in the comments this function fails in some precision, in the case of 1.005 for example it will return 1.00 instead of 1.01. If accuracy to this degree is important I've found this answer: https://stackoverflow.com/a/32605063/1726511 Which seems to work well with all the tests I've tried.

There is one minor modification required though, the function in the answer linked above returns whole numbers when it rounds to one, so for example 99.004 will return 99 instead of 99.00 which isn't ideal for displaying prices.

Edit 3 - Seems having the toFixed on the actual return was STILL screwing up some numbers, this final edit appears to work. Geez so many reworks!

var discount = roundTo((price / listprice), 2);

function roundTo(n, digits) {
  if (digits === undefined) {
    digits = 0;
  }

  var multiplicator = Math.pow(10, digits);
  n = parseFloat((n * multiplicator).toFixed(11));
  var test =(Math.round(n) / multiplicator);
  return +(test.toFixed(digits));
}

See Fiddle example here: https://jsfiddle.net/calder12/3Lbhfy5s/

Edit 4 - You guys are killing me. Edit 3 fails on negative numbers, without digging into why it's just easier to deal with turning a negative number positive before doing the rounding, then turning it back before returning the result.

function roundTo(n, digits) {
    var negative = false;
    if (digits === undefined) {
        digits = 0;
    }
    if (n < 0) {
        negative = true;
        n = n * -1;
    }
    var multiplicator = Math.pow(10, digits);
    n = parseFloat((n * multiplicator).toFixed(11));
    n = (Math.round(n) / multiplicator).toFixed(digits);
    if (negative) {
        n = (n * -1).toFixed(digits);
    }
    return n;
}

Fiddle: https://jsfiddle.net/3Lbhfy5s/79/

2 of 12
172

If you use a unary plus to convert a string to a number as documented on MDN.

For example:+discount.toFixed(2)

🌐
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?
January 8, 2016 -

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.

🌐
CoreUI
coreui.io › blog › how-to-round-a-number-to-two-decimal-places-in-javascript
How to round a number to two decimal places in JavaScript · CoreUI
February 21, 2024 - Another method to round to two decimal places involves multiplying the number by 100 (shifting the decimal point two places to the right), using the Math.round() function, and then dividing back by 100.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › Math › round
JavaScript Math round() - Round Number to Integer | Vultr Docs
December 2, 2024 - Math.round() in JavaScript serves as a straightforward yet powerful tool for rounding numbers to the nearest integer. Its versatility extends from basic rounding of positive and negative values to more complex scenarios where arithmetic manipulation ...
🌐
Ihechikara
ihechikara.com › posts › how-to-round-a-number-in-js
How To Round Numbers in JavaScript – Explained With Code Examples
If the digit after the decimal point is less than 5, then the number will be rounded down to the nearest integer. You can use the Math.floor() method to round a number down to the nearest integer.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Math rounding function
April 14, 2018 - var total = 0; for(var i = 0; i < arr.length; i++) { total += arr[i] } return total / arr.length } function round(number) { return Math.round(total * 100) / 100; } mean([5,8,2,5,8,0,1,4]) The first fun…
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-math-round-method
JavaScript Math round() Method - GeeksforGeeks
September 16, 2024 - It rounds up for decimal values of 0.5 and higher, and down otherwise. ... The Math.round() method returns the value of the given number rounded to the nearest integer.