0

To round a decimal number with the .toFixed() method in JavaScript, I would expect that the ending of ...5 should be rounded up.

As the example shows, in the case of 5.555, the .toFixed(2) method will result in 5.55 although my expectation would be 5.56 due to the rounding standards.

Is any built-in JavaScript or jQuery method for rounding numbers following the international mathematical rounding?


Edit: Using Math.round() is acceptable and understandable, just the number shall be multiplied and divided. I was curious if there is any existing simplified function, like the toFix() just with a solution on the rounding of 5's. Of course it possible to write a function for it or use the Math.round with proper parameterization.

<!DOCTYPE html>
<html>

<body>

  <p>Click the button to display the fixed number.</p>

  <button onclick="myFunction()">Try it</button>

  <p id="demo"></p>

  <script>
    function myFunction() {
      var num = 5.555;
      var n = num.toFixed(2);
      document.getElementById("demo").innerHTML = n;
    }
  </script>

</body>

</html>
questionMark
  • 57
  • 10
  • There are 96 answers on the above question. I think that's sufficient variety. Also, jQuery is a library for manipulating the DOM/CSSOM, not for manipulating numbers. – Heretic Monkey May 03 '21 at 20:15
  • @HereticMonkey Well, I was looking for an exact built-in alternative for .toFix(), like a new function for example: .toFixProperRounding(...). Although Math.round can be used and it's understandable but the rounded number needs to be multiplied and distributed after it. I was expecting a simplified built-in method, like the toFix() with the solution of the above example. – questionMark May 03 '21 at 20:23
  • 1
    There is no function `toFix()`, there is only `toFixed()` which takes a number and produces a string. "An exact built-in alternative"; All of the functions available on `Math` are available [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math). All of the functions available on `Number` are [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number). – Heretic Monkey May 03 '21 at 20:29

2 Answers2

2

You can use Math.round:

let num = 2.555;
console.log(Math.round(num * 100) / 100);

Please see the links below:

How to round to at most 2 decimal places, if necessary?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round

https://www.w3schools.com/jsref/jsref_round.asp

Erfan Bahramali
  • 400
  • 3
  • 12
2

To be precise since you need proper mathematical rounding and not just trimming up to 2 decimal places a better approach will be to use Number.EPSILON

let num = 2.5555;
let result = Math.round((num + Number.EPSILON) * 100) / 100;
console.log(result); //expected 2.56
Subha
  • 416
  • 1
  • 9
  • Actually It will round down numbers like 2.5550 to 2.55, whereas using the Math.round(num * 100) / 100 or num.toFixed(2) rounds it up to 2.56 – bhdrozgn Apr 08 '22 at 14:44