🌐
W3Schools
w3schools.com › jsref › jsref_round.asp
JavaScript Math round() Method
The Math.round() method rounds a number to the nearest integer.
🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Math.round()
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
W3Schools
w3schools.com › jsref › tryit.asp
JavaScript Math
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
W3Schools
w3schools.com › js › js_math.asp
JavaScript Math Object
There are 4 common methods to round a number to an integer: ... Math.floor(4.9); Math.floor(4.7); Math.floor(4.4); Math.floor(4.2); Math.floor(-4.2); Try it Yourself » ... Math.trunc(4.9); Math.trunc(4.7); Math.trunc(4.4); Math.trunc(4.2); Math.trunc(-4.2); Try it Yourself » · Math.sign(x) returns if x is negative, null or positive. ... Math.trunc() and Math.sign() were added to JavaScript 2015 - ES6.
🌐
W3Schools
w3schools.com › jsref › jsref_floor.asp
JavaScript Math floor() Method
The Math.floor() method rounds a number DOWN to the nearest integer.
🌐
W3Schools
w3schools.com › jsref › jsref_ceil.asp
JavaScript Math ceil() Method
The Math.ceil() method rounds a number rounded UP to the nearest integer.
🌐
W3Schools
w3schools.com › jsref › jsref_fround.asp
JavaScript Math fround() Method
❮ Previous JavaScript Math Object ... Math.fround(-2.49); Try it Yourself » · The Math.fround() method returns the nearest 32-bit single precision float representation of a number....
🌐
W3Schools Blog
w3schools.blog › home › javascript math round() method
JavaScript math round() method - W3schools
May 28, 2019 - <!DOCTYPE html> <html> <body> <script> document.writeln(Math.round(45.7864)); </script> </body> </html>
🌐
pawelgrzybek
pawelgrzybek.com › rounding-and-truncating-numbers-in-javascript
Rounding and truncating numbers in JavaScript | pawelgrzybek.com
Rounding and truncating are really necessary for a developer to know and it's a good guide that you have created this article which can guide them on how to do it in Javascript. Reply to essay writing services review ... Thank you very much! I'm glad that you found it useful. Reply to Pawel Grzybek ... // round to the nearest number taking into account the sign const round = number => Math.sign(number) * Math.round(Math.abs(number)) Reply to Mauro Gabriel Titimoli
Find elsewhere
🌐
W3Schools
w3schools.com › js › tryjs_math_round.htm
W3schools
Math.round(x) returns the value of x rounded to its nearest integer:
🌐
W3Schools
w3schools.com › jsref › jsref_tofixed.asp
JavaScript toFixed() Method
The toFixed() method rounds the string to a specified number of decimals. If the number of decimals are higher than in the number, zeros are added. ... If you want to use W3Schools services as an educational institution, team or enterprise, ...
🌐
TechOnTheNet
techonthenet.com › js › math_round.php
JavaScript: Math round() function
This JavaScript tutorial explains how to use the math function called round() with syntax and examples. In JavaScript, round() is a function that is used to return a number rounded to the nearest integer value.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Math › floor
Math.floor() - JavaScript | MDN
It does so by multiplying the number by a power of 10, then rounding the result to the nearest integer, then dividing by the power of 10.
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.

Top answer
1 of 12
1032

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)

🌐
Programiz
programiz.com › javascript › library › math › round
JavaScript Math round()
// round the number to nearest integer let roundedNumber = Math.round(number); console.log(roundedNumber); // Output: 4 ... If the fractional portion > 0.5, x is rounded to integer with higher absolute value. If the fractional portion < 0.5, x is rounded to integer with lower absolute value.
🌐
W3Schools
w3schools.com › js › js_random.asp
JavaScript Random
Math.floor() then rounds down, so you get an integer between 1 and 10. // Returns a random integer from 1 to 100 (both included): Math.floor(Math.random() * 100) + 1; Try it Yourself » · As you can see from the examples above, it might be a good idea to create a proper random function to use for all random integer purposes. This JavaScript function always returns a random integer between min (included) and max (excluded): function getRndInteger(min, max) { return Math.floor(Math.random() * (max - min) ) + min; } Try it Yourself » ·