0

I want to subtract a negative int from a float in javascript:

10.1 - 9

I expected the output to be:

1.1

What I get is:

1.0999999999999996

I get this result in all browsers I have tested (latest Safari, Chrome & Firefox). I tried to convert the int to a float (9.0) or change the order (-9 + 10.1), but it remains the same.

Positive numbers work as expected. Only the negative numbers are affected.

Is there any explanation to this and is it possible to fix this behavior without rounding the result?

Afterlame
  • 1,188
  • 2
  • 13
  • 32
  • 1
    *"Positive numbers work as expected. Only the negative numbers are affected."* Try `0.2 + 0.1`. – Felix Kling Jan 26 '15 at 22:31
  • 1
    Check out this thread for some good information on what you are seeing: http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem – talemyn Jan 26 '15 at 22:33
  • @iamnotmaynard yes, it is. I have not seen this one. Shall I delete this question than? – Afterlame Jan 26 '15 at 22:33
  • http://www.smbc-comics.com/index.php?db=comics&id=2999#comic – imtheman Jan 26 '15 at 22:43

0 Answers0