PDA

View Full Version : parseFloat


pil_uk
09-01-2005, 07:10 AM
Could anybody explain why the following code returns 6.3000000000000001 instead of 6.3?

function test()
{

var t = 0;

t+=parseFloat('1.3')
t+=parseFloat('1.3')
t+=parseFloat('1.3')
t+=parseFloat('1.2')
t+=parseFloat('1.2')
return t
}

Archangel
09-01-2005, 07:25 AM
Not sure about that...

What if you tried

var t = 0;

t = parseFloat( '1.3' + '1.3' + '1.3' + '1.2' + '1.2');

pil_uk
09-01-2005, 08:30 AM
That just returns 1.3. If I remove the quotes it still returns 6.3000000000000001.

Archangel
09-01-2005, 08:54 AM
You can do this....
'-------------------------------------------
function test()
{

var t = 0;

t+=parseFloat('1.3')
t+=parseFloat('1.3')
t+=parseFloat('1.3')
t+=parseFloat('1.2')
t+=parseFloat('1.2')
t = Math.round(t*100)/100
return t
}
'-------------------------------------------

OR

'-------------------------------------------
function test()
{

var t = 0;

t+=parseFloat('1.3')
t+=parseFloat('1.3')
t+=parseFloat('1.3')
t+=parseFloat('1.2')
t+=parseFloat('1.2')
return t.toFixed(2)
}
'-------------------------------------------


Explained in this Google Group...
http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/338c3174c0c7eaf6/0ca6641cd5d75ee1?lnk=st&q=javascript+parseFloat+decimals&rnum=1&hl=en#0ca6641cd5d75ee1

Zee
09-03-2005, 02:13 PM
The reason why that returns 6.300...001 is because of how a computer stores "real" numbers. Basically, in order to fit decimal numbers in 32 and 64 bits (float and double), they had to devise a way of doing that... and that way is exponents. For floats (32 bits), the first bit is the sign bit (+/-) and the following 7 bits are the exponent, and the remaining bits are the mantissa. (For doubles, there's a 10 bit exponent, and the remaining bits are the mantissa.)

Point is, using this representation, some numbers can't be 'exactly' represented. Some numbers will be represented by a little less than the actual number or a little more.

For more information regarding how floating point numbers are handled, google IEEE floating-point represntation, and you're sure to get way more information than you asked for!

Regards

pil_uk
09-06-2005, 09:54 AM
Thankyou for your response Zee. You were correct. I googled and got way more information than I asked for!

Archangel suggested using math.round or toFixed(2) to get around this problem. Is this the best solution or can you offer any other solutions.

Thanks

Zee
09-07-2005, 01:32 PM
Archangel's suggestions are totally fine and llovely!

toFixed is relatively new tho, so if you're looking to support browsers older than netscape 6 and IE 5.5, use the Math.round technique.

Sorry archangel, just noticed you posted a link to a google groups site that explained all of that! My bad, I must've overlooked it thinking it was your signature or something :P.

Regards

Archangel
09-07-2005, 01:33 PM
It's all good :yes:

pil_uk
09-08-2005, 08:22 AM
All working fine now using Math.round.

Thanks for your help guys.