0

I have a problem with rounding float number. Here is 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 : jsfiddle

Have you a solution ? Thank you in advance, cordially

DSDmark
  • 1,045
  • 5
  • 11
  • 25
totoaussi
  • 712
  • 1
  • 11
  • 27
  • 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
  • 22,070
  • 18
  • 81
  • 118
  • 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