Why does the sum of 2 parseFloat variables give me an incorrect decimal number

Viewed 40080

If I have this little function:

<script type="text/javascript">

 function printFloat(){
      var myFloatNumber1 = document.getElementById('floatNumber1');
      var myFloatNumber2 = document.getElementById('floatNumber2');
      alert(parseFloat(myFloatNumber1.value) + parseFloat(myFloatNumber2.value))
 }

</script>

<input type="text" id="floatNumber1"></input>
<input type="text" id="floatNumber2"></input>

<input type="button" onclick="printFloat()"/>

in field 1 I enter: 221.58 in field 2 I enter: 2497.74

I expect the sum of 2 numbers in the input fields to be a 2 number digit: 2719.32 But the result is a incorrect number... : 2719.3199999999997

a round would do the job, but I just don't get why the code does that on this number... On other number combinations, the sum is correct...

3 Answers
Related