Tuesday, March 21, 2006

Javascript rounding weirdness

I have this in code:

var total = parseFloat(txtGLItemAmount1.value) + parseFloat(txtGLItemAmount2.value)

when I insert 110.16 and 7.74 I should get 117.90 but instead get 117.8999999999

Pretty freakin weird! I assume this has something to do with floating point math and all its weirdness. The fix I found for this was this line:

total = total.toFixed(2);

This yeilds the correct value. I bet that I could do this as well to get it correct:

var total = txtGLItemAmount1.value.toFixed(2) + txtGLItemAmount2.value.toFixed(2);


Post a Comment

<< Home