problem with parseFloat - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

problem with parseFloat

SatyajitCSatyajitC Posts: 6Member
Hi all,
I found a weird problem with parseFloat. This is what I tried to do
parseFloat("167.89")+ parseFloat("-67.89").It returned 99.9999988, but not 100.Why is this so?Is this a bug with parseFloat?I know the solution to this problem but not the origin.Can anyone please tell me why this happens or is this a registered bug with SUN?

Comments

  • lillulillu Posts: 308Member
    I've also come across this problem before.
    Here's a thread:

    http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20343444.html

    : Hi all,
    : I found a weird problem with parseFloat. This is what I tried to do
    : parseFloat("167.89")+ parseFloat("-67.89").It returned 99.9999988, but not 100.Why is this so?Is this a bug with parseFloat?I know the solution to this problem but not the origin.Can anyone please tell me why this happens or is this a registered bug with SUN?
    :

    [hr][grey]To err is human, but to really foul things up requires a computer. (Farmers Almanac)[/grey] :-)

  • WeirdofreakWeirdofreak Posts: 439Member
    You're either thinking Java here, or you've got the wrong company. Sun did Java, Netscape did Javascript based on Java.

    Still, the problem is that floating-point numbers are hard to store accurately, making them hard to work with.
  • SatyajitCSatyajitC Posts: 6Member
    Fine,but is there a way by which I can predict for which floating point literals this error may occurr? I tried to search Netscape for this,but with no luck.Is this registered somewhere?
  • lillulillu Posts: 308Member
    No there's no available function in javascript. If precision is not an issue, you can round off the number to the decimal place you want.
    Eg. if you want 2 decimal places remaining use this:

    Math.round(n * 100) / 100

    n is the number returned by ParseFloat you want to round
    100 rounds to 2 decimal places
    1000 rounds to 3 decimal places and so on...

    : I've also come across this problem before.
    : Here's a thread:
    :
    : http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_20343444.html
    :
    : : Hi all,
    : : I found a weird problem with parseFloat. This is what I tried to do
    : : parseFloat("167.89")+ parseFloat("-67.89").It returned 99.9999988, but not 100.Why is this so?Is this a bug with parseFloat?I know the solution to this problem but not the origin.Can anyone please tell me why this happens or is this a registered bug with SUN?
    : :
    :
    : [hr][grey]To err is human, but to really foul things up requires a computer. (Farmers Almanac)[/grey] :-)
    :
    :

    [hr][grey]To err is human, but to really foul things up requires a computer. (Farmers Almanac)[/grey] :-)

Sign In or Register to comment.