tags:

views:

32

answers:

2

I've got a weird maths/rounding problem in Javascript.

The snippet below is a very basic example of the code I'm running. Obviously it's not this exact code, but I'm taking a decimal value from a text box, working out a percentage and taking this away from one.

var ten = "10";
var eight = "8";

alert(1 - (eight/ten));

The problem is the answer is 0.2 but the calculation returns 0.1999999999999996. Yet if I do 1 + (eight/ten) 1.8 is returned. What is going on?

+3  A: 

It's because of the way floating point numbers are represented.

Darin Dimitrov
+2  A: 

Welcome to the world of floating-point numbers!

Computers don't actually work with decimal numbers--i.e. numbers in base ten, the way we use them in normal life. They work with binary numbers, and that includes floating-point numbers. They're represented in binary as well, and that means that "number of decimal places" is not always a meaningful property of a floating-point number.

For instance, a floating-point number cannot exactly represent 0.1, and you'll get something like 0.1000000001 if you try to use it in your code. The exact value you get varies by implementation, and is not correctable by subtracting the difference, as the computer can't tell that there is a difference--that's as close as it can get to 0.1.

(Stole most of this answer from a previous answer of mine.)

Jonathan Grynspan