4 Replies Latest reply on Oct 20, 2007 11:50 AM by keyboardlacky

    Flex Can't Add!

    keyboardlacky Level 1
      I've created an object that extends the textbox to accept only currency values. In it I have a .numericValue property that accepts or returns a Number that represents the actual numeric value of that object.

      I've made a form that has a messy binding that adds up several of these objects. It looks like this:

      quote:

      <mx:Binding destination="item_220.numericValue" source="item_201.numericValue + item_202.numericValue
      + item_203.numericValue + item_204.numericValue
      + item_205.numericValue + item_206.numericValue + item_207.numericValue + item_208.numericValue
      + item_209.numericValue + item_210.numericValue + item_211.numericValue + item_212.numericValue
      + item_213.numericValue + item_214.numericValue + item_215.numericValue + item_216.numericValue
      + item_217.numericValue + item_218.numericValue + item_219.numericValue"/>


      However, the math is wrong! If I put eleven cents in several of the objects, the result is correct

      For example:
      0.11 + 0.11 + 0.11 + 0.11 + 0.11 = 0.55 (as expected)

      However, if I increase the value, it sometimes misses a penny

      For example:
      1111.11 + 0.11 + 0.11 + 0.11 + 0.11 = 1111.54 (not expected!)

      However, it is not consistent.

      For example:
      1000.11 + 0.11 + 0.11 + 0.11 + 0.11 = 1000.55


      I've tried forcing the equation by wrapping each object with the Number() method, but this does not change the result.

      For example: Number(item_216.numericValue) + Number(item_216.numericValue)

      Please help! Thanks,
      David
        • 1. Re: Flex Can't Add!
          AaffordableWeb Level 1
          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.
          • 2. Re: Flex Can't Add!
            ebrentnelson Level 1
            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.
            • 3. Re: Flex Can't Add!
              keyboardlacky Level 1
              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.
              • 4. Re: Flex Can't Add!
                keyboardlacky Level 1
                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.

                Thanks everyone,
                David