Monday, June 3, 2024
129
rated 0 times [  136] [ 7]  / answers: 1 / hits: 31680  / 13 Years ago, tue, may 17, 2011, 12:00:00

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 ?


More From » floating-point

 Answers
5

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.


[#92202] Sunday, May 15, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alexandreah

Total Points: 720
Total Questions: 85
Total Answers: 90

Location: Central African Republic
Member since Fri, Jun 5, 2020
4 Years ago
;