5 Replies Latest reply on Jul 22, 2010 5:13 AM by rootsounds

    Problem Currency Formatting > 20 digit numbers

    NiladriSadhukhan

      Hi,

      Can any one suggest how to format currency for long numbers say 18 to 20 digits(decimal). I find Flex Number is not able to deal with such long amount formatting, so how to achieve the same in Flex.

        • 1. Re: Problem Currency Formatting > 20 digit numbers
          rootsounds Level 4

          Please share an example.


          • 2. Re: Problem Currency Formatting > 20 digit numbers
            NiladriSadhukhan Level 1

            Try below with say : 123456789012345.99 

            <mx:Script>

                    <![CDATA[

             

                          import mx.events.ValidationResultEvent;           

                          private var vResult:ValidationResultEvent;

             

                          // Event handler to validate and format input.

                          private function Format():void {

             

                                 vResult = numVal.validate();

             

                                if (vResult.type==ValidationResultEvent.VALID) {

                                    var temp:Number=Number(priceUS.text);

                                    formattedUSPrice.text= usdFormatter.format(temp);

                                }

             

                                else {

                                   formattedUSPrice.text="";

                                }

                          }

                    ]]>

                </mx:Script>

             

                <mx:CurrencyFormatter id="usdFormatter" precision="2"

                    currencySymbol="$" decimalSeparatorFrom="." rounding="none"

                    decimalSeparatorTo="." useNegativeSign="true"

                    useThousandsSeparator="true" alignSymbol="left"/>

             

                <mx:NumberValidator id="numVal" source="{priceUS}" property="text"

                    allowNegative="true" domain="real"/>

             

                <mx:Panel title="CurrencyFormatter Example" width="75%" height="75%"

                    paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

             

                    <mx:Form width="100%">

                        <mx:FormItem label="Enter U.S. dollar amount:" width="100%">

                            <mx:TextInput id="priceUS" text="" width="50%"/>

                        </mx:FormItem>

             

                        <mx:FormItem label="Formatted amount: "  width="100%">

                            <mx:TextInput id="formattedUSPrice" text="" width="50%" />

                        </mx:FormItem>

             

                        <mx:FormItem>

                            <mx:Button label="Validate and Format" click="Format();"/>

                        </mx:FormItem>

                    </mx:Form>

            • 3. Re: Problem Currency Formatting > 20 digit numbers
              rootsounds Level 4

              I've confirmed from your example that it is rounding despite being instructed not to do so. It must be converting the field to a number before operating on it rather than just formatting a String.

               

              Since you have a number validator checking the input, all you need to do is strip out all non-numeric characters except for the decimal point. Then apply whatever formatting you like to the String through looping and fixed positions.

              • 4. Re: Problem Currency Formatting > 20 digit numbers
                NiladriSadhukhan Level 1

                Actually the root problem is something else. In my app I'v a form where user enters amount in number and on focus out I format it to display the same in currency string and on focus In I strip the string and convert it to Number. But I have to perform arithmetic operations on the number only.

                Now it looks like the Number datatype runs out of capacity for an amount say : 123456789012345.99.

                sample :

                var temp:Number= 123456789012345.99;

                Alert.show((temp+1)+'');

                 

                actual result     : 123456789012346.99

                expected result : 123456789012346.98

                 

                 

                So how to deal with the looooong figures and how to process them !!