I was wondering how can I subtract two negative Floating-Point numbers in javascript. I tried:
alert(-0.2-0.1);
and the result is -0.30000000000000004. Am I doing something wrong? What do I have to do to get -0.3 ?
-0.30000000000000004
-0.3
No, nothing wrong with your code, most decimal fractions cannot be represented exactly in binary, use
number.toFixed(x)
Where x is the number of decimals you want and number is the result of the subtraction.
x
number
2.1m questions
2.1m answers
60 comments
57.0k users