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.
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