Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actionscript's Number class' toFixed() method returns strange values

I'm doing some calculation with numbers and would like to only print an integer (the number without the decimals).

I thought that the toFixed() method of the Number class would do the trick, and it does most of the time. But quite frequently strange values are returned. Here are 2 examples:

  • Number(0.002).toFixed(0) returns "0." while Number(1.002).toFixed(0) returns "1" (without the period)

  • Once in a while, Number(0.002).toFixed(0) returns "1"

Needless to say that's not the expected behaviour. Am I not using this method correctly?

**edit: ** I know I just have to do int(0.002) to get 0 but I'd like to understand that strange behaviour.

like image 820
Guillaume Flandre Avatar asked Mar 02 '26 08:03

Guillaume Flandre


1 Answers

This is a known bug. The unsexy workaround is to use either use Math.round() or just checking the returned string for that trailing period.

See bug report on JIRA here: http://bugs.adobe.com/jira/browse/FP-1595

like image 174
grapefrukt Avatar answered Mar 04 '26 19:03

grapefrukt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!