开发者

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

开发者 https://www.devze.com 2022-12-15 23:37 出处:网络
I\'m doing some calculation with numbers and would like to only print an integer (the number without the decimals).

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:

  • Nu开发者_JAVA技巧mber(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.


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


I've tested both on Flex SDK 3.4 and Flex SDK 4.0 and it looks like a bug, maybe post it on bugs.adobe.com? Although I think it might be related to the Flash Player rather than Flex.

Anyway if you're trying to convert a number to a string with no decimal places you can use this as a workaround:

Math.round(theNumber).toString()

This will get rid of all the digits after the dot.


Just FYI for those reading this question: obviously this is a known bug, but I wanted to mention I'm seeing this issue with the Flex 4.0 SDK as well as the current Flex 4.1 SDK. On the Adobe bugs site I've asked for clarification on when this was fixed (the ticket says "Resolved: 02/26/09 01:28 PM" but not which version allegedly contains the fix).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号