This content has been marked as final. Show 4 replies
Floating point math (Number) has always had errors. The easiest way to get it right is to multiply by 100 and convert to int, do the math, divide the answer by 100 and convert to Number. This usually solves this type of problem.
There is an attribute on the TextInput control called restrict (set it to "0123456789." to only allow numeric input). Use that to only accept numeric values (or a numericStepper). Then make sure you cast everything you are adding to a Number or int (else you will be concatenating strings "11" + "11" = "1111", 11 + 11 = 22). If I were you, I would handle all of this in a function, or add all the values to an Array and write a sum function that takes an Array as a parameter.
Thanks guys. I was just in the process of creating a new integer property that equalled the value*100. I knew floating point was iffy, but I though the input values had to be much larger before they started acting flakey.
I'm going to have to totally refactor this monstrosity soon. I've got a class that extends the currencyFormatter... Nelson's tip can eliminate a lot of it's functionality. There's some strange stuff going on.
I've got it working (as far as I can ascertain) with a combination of multiplying by 100 and adding small values (0.000001) and rounding. It's ugly, really.
I wish Flex had a datatype that could natively support currency with accuracy & precision.