0

I have a problem with rounding float number. Here the source code, very simple :

a = "2.3";
result = parseFloat(a)+0.01
console.log(result);

Console displays 2.3099999999999996 instead of 2.31. You can try here : https://jsfiddle.net/fh9bj83u/

Have you a solution ?

Thank you in advance, cordially

totoaussi
  • 683
  • 1
  • 8
  • 23
  • It's not clear what "problem" you want to solve. The difference between 2.3.0999... and 2.31 is insignificant for any calculations. – Stephen Thomas Sep 24 '15 at 15:14
  • No is significant, because in my bill, I must display the number with 2 digits after the dot after rounding. – totoaussi Sep 24 '15 at 15:19

1 Answers1

3

Use toFixed to trim to 2 decimal places.

result = (parseFloat(a)+0.01).toFixed(2)
"2.31"
Sterling Archer
  • 21,348
  • 17
  • 79
  • 113
  • Thank you very much, you deserve +100000000, solved. toFixed() method is magic because it avoid me to use substr() method. – totoaussi Sep 24 '15 at 15:23