14 Replies Latest reply on Aug 30, 2010 7:48 AM by MoonlightWare

    Flex drops out decimal zeroes??

    rock-

      Hello, I´m facing a problem like this:

       

      php file returns output like this (example.php):

       

      <tests>

      <test1>This is a string<test1>

      <test2>100.03<test2>

      <test3>100.00<test3>

      </tests>

       

      In the mxml file I have this (data.mxml):

       

      <mx:HTTPService id="userRequest" url="./php/text/example.php" result="_resultContainerOn=0;">

      <!-- and some non-important stuff -->

      </mx:HTTPService>

       

      <mx:DataGrid id="resultTable" dataProvider="{userRequest.lastResult.tests}" >

                                  <mx:columns>
                                      <mx:DataGridColumn headerText="test1" dataField="test1" />
                                      <mx:DataGridColumn headerText="test2" dataField="test2"/>
                                      <mx:DataGridColumn headerText="test3" dataField="test3/>

                                  </mx:columns>

      </mx:DataGrid>

       

      And the result in UI:

      test1                   test2     test3    

      This is a string     100.03  100

       


      How can I force the "100.00" to be a string so Flex wouldn´t handle it as a number and drop out the decimal zero?

       

      Thank you very much for your answers and professional knowledge!

        • 1. Re: Flex drops out decimal zeroes??
          supersonicecho

          use the toFixed method of the Number class.

           

          See http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Number.html

           

          E.G.

          var goodString = number.toFixed(2);

          • 2. Re: Flex drops out decimal zeroes??
            Flex harUI Adobe Employee

            What is the data type of the items in the dataProvider?

            • 3. Re: Flex drops out decimal zeroes??
              rock- Level 1

              The php file collects the data from database and returns the data in XML format as shown in the first post.

               

              The datatypes are all defined as CHAR in the database.

               

              There are no other data type definitions I think.

               

              The problem is that when the data goes from PHP file to the Dataprovider, the Dataprovider thinks that "100.00" is a number but I would like to force it to be a string.

              • 4. Re: Flex drops out decimal zeroes??
                supersonicecho Level 2

                Again, use toFixed(2)

                 

                <mx:DataGrid id="resultTable" dataProvider="{userRequest.lastResult.tests}" >
                                            <mx:columns>
                                                <mx:DataGridColumn headerText="test1" dataField="test1.toFixed(2)" />
                                                <mx:DataGridColumn headerText="test2" dataField="test2.toFixed(2)"/>
                                                <mx:DataGridColumn headerText="test3" dataField="test3.toFixed(2)"/>
                                            </mx:columns>
                </mx:DataGrid>
                

                 

                If that doesn't work, try casting the data as a number first, E.G.

                 

                <mx:DataGrid id="resultTable" dataProvider="{userRequest.lastResult.tests}" >
                                            <mx:columns>
                                                <mx:DataGridColumn headerText="test1" dataField="Number(test1).toFixed(2)" />
                                                <mx:DataGridColumn headerText="test2" dataField="Number(test2).toFixed(2)"/>
                                                <mx:DataGridColumn headerText="test3" dataField="Number(test3).toFixed(2)"/>
                                            </mx:columns>
                </mx:DataGrid>
                

                • 5. Re: Flex drops out decimal zeroes??
                  rock- Level 1

                  toFixed() has this problem:

                   

                  Here are some examples the <test3> element has as data:

                   

                  <test3>100.00</test3>

                  <test3>100KKOOO2211114.00</test3>

                  <test3>SK100029.0000</test3>

                   

                  Now when it is used with toFixed(2) the data which has 4 decimals would be only with 2 decimals. I need to have the data exactly same as it is in the XML format.

                  • 6. Re: Flex drops out decimal zeroes??
                    supersonicecho Level 2

                    Ok got ya. 

                     

                    This is strange because I thought XML was cast by actionscript as a string literal.

                     

                    Well, I would try casting the data as a String in the dataField="{String(test3)}".

                     

                    If that doesn't work, try using a custom labelFunction for the dataColumn to prevent the default labelFunction from improperly casting your data type.

                    • 7. Re: Flex drops out decimal zeroes??
                      rock- Level 1

                      dataField="{String(test3)}" // That didn´t work unfortunately.

                       

                       

                      But about the labelFunction, got any examples how the labelFunction should look like?

                       

                      <mx:DataGridColumn headerText="test3" dataField="test3" labelFunction="preventNumberCasting" />

                       

                      private function preventNumberCasting(item:Object, col:DataGridColumn ):String
                      {

                           // what shall we put here?

                      }

                      • 8. Re: Flex drops out decimal zeroes??
                        supersonicecho Level 2

                        Try testing out what format the data is in.

                         

                         

                        private function preventNumberCasting(item:Object, col:DataGridColumn ):String
                        {

                         

                             trace(item);

                         

                             if(item is String) trace('is string');

                         

                             return String(item);

                        }

                        • 9. Re: Flex drops out decimal zeroes??
                          rock- Level 1

                          Here are the datatypes:

                           

                          This is a string -> String


                          100.03  -> Number


                          100.00 -> Number

                           

                          SK100029.0000  -> String

                           

                           

                          They all should be forced to string.

                           

                          What now?

                          • 10. Re: Flex drops out decimal zeroes??
                            fleRavix

                            hi,

                             

                            if it is 100.00 -> int -> 100

                            if it is 100.123 -> Number -> 100.123

                            if it is sk100.2233 -> string -> sk100.2233(max chars of the string)

                             

                            var Num:Number = 100.100;

                            StrNum = Num.toFixed(decPlaces);

                            return StrNUm;

                             

                            Mark this if helpful

                            • 11. Re: Flex drops out decimal zeroes??
                              rock- Level 1

                              I couldn´t get the above working.

                               

                              Is the problem that XML Decoder drops out the decimals? XML Decoder thinks "100.00" is a int and makes it to int to the dataprovider?

                               

                              Do I have to some how change how the internal XML decoder works?

                               

                              Is there any other solutions?

                              • 12. Re: Flex drops out decimal zeroes??
                                jomelia

                                If your data are purely numbers, use a NumberFormatter in a labelFunction:

                                 

                                <mx:NumberFormatter id="numberFormatter" precision=2" thousandsSeparatorTo=","decimalSeparatorTo="." />

                                 

                                private function formatNbr(item:Object,column:DataGridColumn):String{
                                            
                                                 return numberFormatter.format(item.test1);;
                                            }

                                 

                                Thanks,

                                Jack

                                • 13. Re: Flex drops out decimal zeroes??
                                  rock- Level 1

                                  The data is not purely numbers.

                                   

                                  What about this one?

                                   

                                  <mx:HTTPService id="userRequest" xmlDecode="saveDecimals" resultFormat="object" url="./php/text/example.php"  result="_resultContainerOn=0;">

                                  </mx:HTTPService>

                                   

                                  private function saveDecimals( myXML:XMLDocument ):Object{

                                   

                                       // and here we could force all elements in tests to be string?

                                  }

                                   

                                   

                                   

                                  Any ideas how should the saveDecimals function look like in this case? When the data in example.php is like:

                                   

                                  <tests>

                                  <test1>This is a string<test1>

                                  <test2>100.03<test2>

                                  <test3>100.00<test3>

                                  </tests>

                                  • 14. Re: Flex drops out decimal zeroes??
                                    MoonlightWare

                                    Use labelFunction only on the columns that you want not all columns have to have a labelFunction. I use it all the time for control how the data is displayed.