This content has been marked as final. Show 7 replies
use the toFixed method of the Number class. Keep in mind that this returns a string value so you'll have to type cast it back to a number.
var myNumber:Number = new Number(999.43);
var result:Number = myNumber - 999;
var precise:String = result.toFixed(2);
result = Number(precise);
Ah thanks, that is nice to see for AS3, but I am using AS2 here still. Just out of curiosity, would you know why it is doing this?
it's an issue of precision. I believe you can still do something similar in AS2. I believe the Number class in AS2 allows you to convert a number to a string which you could then concatenate with the Sring class methods and convert back to a number.
the reason for the apparent error is because flash is performing arithmetic on a binary computer. within the limits of flash'es accuracy (16 digits),
0.42999999999995 = .43
to eliminate those errors, you can use integer arithmetic or the flash string methods. integer arithmetic is probably preferable if you're doing arithmetic operations.
ah...I understand now, thanks to the both of you.
you're welcome.1 person found this helpful