Floating amount with javascript

Possible duplicate:
Is math javascript broken?

I am calculating the sum of several float values ​​using javascript and ... I noticed a strange thing that was not seen before. The execution of this code is:

parseFloat('2.3') + parseFloat('2.4') 

I get 4.69999999999999999

So ... what can I do to get the right value? (assuming this is not true ...)

+42
javascript math floating-point
Sep 20
source share
2 answers

Once you read that What every computer scientist needs to know about floating point arithmetic , you can use .toFixed() :

 var result = parseFloat('2.3') + parseFloat('2.4'); alert(result.toFixed(2));​ 
+86
Sep 20 '12 at 10:50
source share
 (parseFloat('2.3') + parseFloat('2.4')).toFixed(1); 

he is going to give you a solution, I guess

+6
Sep 20
source share



All Articles