PDA

View Full Version : Rounding inconsistancy



NancyJ
01-23-2011, 12:40 PM
num = 1.025;
alert(num.toFixed(2)); // - comes out as 1.02
num = 0.025;
alert(num.toFixed(2)); // comes out as 0.03;


Why is this?

Philip M
01-23-2011, 01:12 PM
num = 1.025;
alert(num.toFixed(2)); // - comes out as 1.03 in IE8


It's due to the way floating point numbers are represented as binary numbers by computers. These numbers cannot be represented exactly.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.