Forum Moderators: open

Message Too Old, No Replies

Javascript - 52.15 / 100 => 5.2149 why?

Divided by 100 error

         

javatips

8:03 am on Mar 15, 2005 (gmt 0)



Please help.

I do not know why when I use javascript to figure out 52.15/100, I get 5.2149. How to solve it?

SpaceFrog

8:19 am on Mar 15, 2005 (gmt 0)

Bernard Marx

11:15 am on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That thread is largely concerned with numbers as strings of digits. This is caused by a binary rounding error. I'm not sure there is a totally generic solution.

This won't help much, but it will leave you in a state of knowing confusion:
[merlyn.demon.co.uk...]

Bernard Marx

11:36 am on Mar 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking more closely...

52.15/[red]100[/red] --> 5.2149 

This is bad behaviour indeed!
I'm thinking you mean:

52.15/[blue]10[/blue] --> 5.2149

I have tried this calculation with various Javascript/JScript interpreters, and can't reproduce the problem. All results are:

5.215

What are you running this in?

Asif

9:45 pm on Mar 16, 2005 (gmt 0)

10+ Year Member



how about trying to round your results? did you try that? i'm not sure if it will help much though, depending upon what kind of problems you face when trying other calculations (there should be a general pattern)