0

I hope theres a javascript expert that can help. I am trying to round numbers to 2 decimal places example

3.342 = 3.34

4.565 = 4.57

means from the third digit is 5 and above to round to the next decimal, and with no change below 5 I have something like

var num = Math.round(n*100)/100

and it works for other numbers but not for n = 4.225 which rounded would be 4.23 and because I couldn't find the issue I printed n*100 this is 4.225*100 and it turns I got 422.49999999999994 instead of 422.5 and that explains why Math.round gives me 4.22 instead of 4.23.

please, can anybody give some light on this issue?

This is happening with other numbers too and I have no solution yet. I also added math.Epsilon like

(n+math.Epsion) * 100

GetSet
  • 11
  • 2
    This is the classic [0.1 + 0.2 == 0.30000000000000004](https://0.30000000000000004.com/) problem. Floating point numbers just aren't precise. The only way you can have perfect precision is either with your own number implementation, or with strings. – Samathingamajig Aug 24 '21 at 00:31
  • 2
    If you're rounding for the purpose of displaying the number, consider using [Number.prototype.toFixed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed). – edemaine Aug 24 '21 at 00:32
  • @edemaine `toFixed` has the same problem with rounding – coagmano Aug 24 '21 at 00:33
  • @FredStark `4.225.toFixed(2)` returns `'4.22'` which is at least the correct length. With `toFixed`, OP likely doesn't need to multiply or divide, avoiding floating point issues in the formatting process. (Of course, there might be inaccuracies before that point.) The limitation here is that you get a string, not a floating point number. – edemaine Aug 24 '21 at 00:36
  • 1
    Even though you get a string if you give that to `Math.round` it still works to get the right answer: `Math.round((4.225*100).toFixed(1)) / 100` – LeeLenalee Aug 24 '21 at 00:38
  • 1
    @edemaine OP already said that 4.565 rounds correctly but is asking about `4.225`, which `toFixed` returns `'4.22'` where we would expect `'4.23'` – coagmano Aug 24 '21 at 00:38
  • @LeeLenalee that sounds like an answer, want to post it as one and I'll upvote it? – coagmano Aug 24 '21 at 00:39

1 Answers1

1

You can use Math.round combined with toFixed to solve your problem:

console.log(Math.round((4.225*100).toFixed(1)) / 100)
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
  • I still got 4.22 and so frustrated with this issue, I tested it in the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed site also in the https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_round site and it worked fine in the first one, no the second one, either on my code. – GetSet Aug 24 '21 at 07:55
  • I solve the problem like this: function roundToTwo(num) { return +(Math.round(num + "e+2") + "e-2"); } console.log(roundToTwo(2.005)); Not my code, and I couldn't find it on the web either, this is a contribution of other developers and honestly I don´t understand the process behind. But I share with you hoping you don't get as frustrated as I was. And if someone understand the math, logic and processing behind please share. I wish I can post it like an Answer, I just don't know how. – GetSet Oct 26 '21 at 21:38