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
  • 97
  • 2
  • 11
  • 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

Please also read the following links for handle floating-point arithmetic:

Is floating point math broken?

How to deal with floating point number precision in JavaScript?

Erfan Bahramali
  • 392
  • 3
  • 13
  • This does not work for 35.855, it prints "35.85" when it should print "35.86" – Simon Tran Jun 21 '23 at 15:12
  • The reason for this is due to the way that JavaScript (and many other programming languages) handle floating-point arithmetic. Internally, floating-point numbers are represented using a binary format called IEEE 754. While this format can represent most decimal values accurately, some decimal values cannot be represented exactly in binary. This can result in rounding errors and small discrepancies when performing arithmetic operations on floating-point numbers. – Erfan Bahramali Jun 25 '23 at 17:23
  • In the case of 35.855 * 100 in JavaScript, the intermediate result computed by the engine may not be exactly equal to the expected value of 3585.5, but rather a close approximation of it. When this approximate value is converted back to a decimal representation, it can result in a small rounding error, causing the final result to be 3585.4999999999995 instead of 3585.5. To avoid these kinds of issues, it's often recommended to use special libraries or data types that allow for more precise handling of decimal numbers, such as the built-in BigInt type or third-party libraries like decimal.js. – Erfan Bahramali Jun 25 '23 at 17:23
  • https://stackoverflow.com/questions/588004/is-floating-point-math-broken https://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript – Erfan Bahramali Jun 25 '23 at 17:25
  • I understand the reasoning, but it should probably be mentioned in the answer, since libraries like lodash's `_.round()` can handle it – Simon Tran Jun 27 '23 at 13:08
  • I added in the answer – Erfan Bahramali Jun 29 '23 at 07:24
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
  • 562
  • 2
  • 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