The problem lies between using Number
which is a float type (not integer type). Floating point arithmetic is imprecise and usually rounded to give what appears to be correct value. For example 0.08 + 0.08
gives correct 0.16 and so does 0.07 + 0.07
giving correct 0.14, so why does 0.07 + 0.08
not give correct 0.15? A Float Rounding Error gives you that 0.15000000000000002 result.
Remembertrace
shows Strings (which can hold all the digits) so you get the system printing out the full 0.15000000000000002 (errors included) which leads to confusion as to what your true value is.
The easiest fix : Use myNum.tofixed(2);
for two decimal places.
var a:String = "0.07";
var b:Number = 0.08;
trace( ( Number(a) + Number(b) ).toFixed(2) ); //gives 0.15
The correct fix : Use whole numbers (integers) & Divide by 100
var a:String = "7";
var b:Number = 8;
trace( ( Number(a) + Number(b) ) /100 ); //gives 0.15
Think of it like cents & dollars. Since you have three digits for float dollars as 1.75
that can be same as 175
in integer cents. To get the 1.75
from our integer cents we do 175 / 100 = 1.75
. Divide by 100 to get two decimal places, divide by 1000 for three decimal places and so on...
To add that fractined value back to a Number
just do something like :
var myNum : Number = ( Number(a) + Number(b) ) /100;
trace("myNum : " + myNum); //gives myNum : 0.15