5 Replies Latest reply on Jul 11, 2010 11:21 AM by John3Others

# What is the meaning of this???

I'v been having a lot of trouble when adding decimal values inside Flex, so I was browsing help and reading the Number Data type definitión and founded this text:
"If your application requires absolute precision with decimal numbers, you need to use software that implements decimal floating point arithmetic as opposed to binary floating point arithmetic."

Can someone elaborate on this topic, I realy need to know what other solutions are there. Can Foting point numbers be stored in some other data type? or is there a routine y can use? or does anyone knows how to round this values for example: 4069.2799999999997 round into 4069.28, please I urge help on this topic. :S
• ###### 1. Re: What is the meaning of this???
Use <mx:NumberFormatter>

TS
• ###### 2. Re: What is the meaning of this???
Number formatter will do the rounding fine, but this doc note means that the AS implementation cannot work with tiny decimal amounts because of the way the language works. If accuracy at those distant decimal positions is important, you will need to do something else.

There may be a library that will help.

Tracy
• ###### 3. What is the meaning of this???
It isn't as much the fact that it's inaccurate at those distant decimal places, as it is just a basic shortcoming of binary floating point arithmetic in general when representing a decimal number. Case in point:

var myDecimal:Number = (0.1 + 0.2 + 0.3) - 0.6;

myDecimal is expected to be 0, but it isn't and not because there are many decimal places in any one of the numbers that were added. It is because 0.1 or 0.2, etc. cannot have a representation in binary FP that is exact.

Whenever I work with decimal places where equality is checked, I define a threshold that would be within an acceptable range of accuracy.

TS
• ###### 4. What is the meaning of this???
You could always use Math.round() with multiplication. Ie. multiply you number by the precision you need, round, then divide back out. I'm sure there are better algorithms, but that would work.

numberFormatter is generally for Strings and is probably a lot more expensive than even my simple algorithm.
• ###### 5. Re: What is the meaning of this???

It's now over two years after the original post, and there's still no decimal type. Even Python has a decimal type now, and it's been standardized (at least by ANSI) for quite a few years. There really ought to be a decimal data type. Without one, it's difficult to ensure industrial-strength code: you rely all the time on the programmers' understanding of where the problems can lie, and how to fix them. And this while dealing with money! Come on, Adobe, please step up to the plate on this one.