Javascript dividing by 100 giving long decimal numbers

joshy

New Member
Messages
8
Reaction score
0
Points
0
I have been working on a calculation website for school. I have to divide numbers by 100 to convert from percentage to decimal. With some numbers, I get continuous decimal numbers, which by a normal calculator, doesn't happen.

Here's some examples (good and bad):
Code:
0.13 / 100 = 0.0013 (good)
0.14 / 100 = 0.0014000000000000002 (bad)
0.141 / 100 = 0.0014099999999999998 (bad)
0.145 / 100 = 0.00145 (good)
0.67 / 100 = 0.0067 (good)
0.68 / 100 = 0.0068000000000000005 (bad)

Also, if multiplied them by 10 (making 0.68 become 6.8 for example) then divided by 1000:
Code:
0.68 * 10 = 6.8
6.8 / 1000 = 0.0068 (good)
0.141 * 10 = 1.41
1.41 / 1000 = 0.00141 (good)
(Could be an issue with the '0')

I'm using JavaScript to code and Safari to view.

Any ideas, or should I try the multiply by 10 method?

EDIT: Have realised this is a common Javascript problem called a rounding error, and it's not just with 0's.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
It's not a JS error, it's a consequence of floating point representation. For the same reason that the decimal expansion of 1/3 doesn't have a finite representation in base 10, numbers such as 1/5 don't have a finite representation in base 2 (which would be 0.00110011...₂). It's just that JS is truncating/rounding to hide the error. Floating point operations can compound this error (how much depends on the operations and arguments), which is likely why you see it with some calculations and not others, even when the resulting value should be the same.

Code:
(0.2).toPrecision(17);
// result is "0.20000000000000001"

If JS is including non-significant digits when displaying the numbers, you'll have to perform rounding yourself.

See "What every computer scientist should know about floating-point arithmetic" for the full story.

@hrvat: how is that relevant?
 
Last edited:
Top