1

I can't figure out why is it so:

var a:String = "0.07";
var b:Number = 0.08;
trace( Number(a) + Number(b) ); // 0.15000000000000002

, but when using tenths, results are ok:

var a:String = "0.7";
var b:Number = 0.8;
trace( Number(a) + Number(b) ); // 1.5
kolyaseg
  • 535
  • 5
  • 13
  • Possible duplicate of [How to deal with floating point number precision in JavaScript?](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) – null May 18 '16 at 16:32
  • Welcome to floating point arithmetic. This has been answered [here](http://stackoverflow.com/questions/1458633/how-to-deal-with-floating-point-number-precision-in-javascript) for JavaScript already and the same ideas apply to as3. – null May 18 '16 at 16:33

1 Answers1

1

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
VC.One
  • 14,790
  • 4
  • 25
  • 57