1 Reply Latest reply on Mar 12, 2010 4:19 AM by jimmyoneshot

    Working With Prices/Decimal Points

    jimmyoneshot Level 1

      I've recently figured out how to perform calculations on values by assigning them to variables in a click function however I'm working with prices in my app and it seems difficult to get prices/decimal points to claculate properly. At the moment my app looks like this:-

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application
      
       xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
      
      <mx:Script>
      <![CDATA[
      
      
      private function getTotal():void{
      
      var addon1Int:int = parseInt(addon1.text);
      
      var addon1Float:Number = parseFloat(addon1.text);
      
      var addon1Number:Number = new Number(addon1.text);
      
      
      var addon2Int:int = parseInt(addon2.text);
      
      var addon2Float:Number = parseFloat(addon2.text);
      
      var addon2Number:Number = new Number(addon2.text);
      
      
      var addon3Int:int = parseInt(addon3.text);
      
      var addon3Float:Number = parseFloat(addon3.text);
      
      var addon3Number:Number = new Number(addon3.text);
      
      
      var addon4Int:int = parseInt(addon4.text);
      
      var addon4Float:Number = parseFloat(addon4.text);
      
      var addon4Number:Number = new Number(addon4.text);
      
      
      var addon5Int:int = parseInt(addon5.text);
      
      var addon5Float:Number = parseFloat(addon5.text);
      
      var addon5Number:Number = new Number(addon5.text);
      
      
      var quantityNumber:Number = new Number(quantity.value); 
      total.data = (addon1Number + addon2Number + addon3Number + addon4Number + addon5Number) * (quantityNumber);
      }
      
      ]]>
      
      </mx:Script>
      
      
      <mx:Text id="addon1" text="5.99"/>
      
      <mx:Text id="addon2" text="6.99"/>
      
      <mx:Text id="addon3" text="8.99"/>
      
      <mx:Text id="addon4" text="4.99"/>
      
      <mx:Text id="addon5" text="3.99"/>
      
      
      <mx:NumericStepper id="quantity"/>
      
      
      <mx:Button label="Button" click="getTotal();"/>
      
      
      <mx:Text id="total"/>
      
      </mx:Application>
      
      
      
      
      


      It works ok if the numbers in the text areas are whole numbers like 8 or 4 but if I use numbers like 02.99, 05.09 the result comes out a bit srange. For example the result of the numbers that I have in there now if I set the value to 2 in the numeric stepper is 61.900000000000006 when I know from using a real calculator it should be 61.9. Is there any way perhaps of rounding this code to the nearest decimal point so it appears as 61.90?

       

      Are there any good tutorials that any of you know of when working with prices/decimal points in flex?

        • 1. Re: Working With Prices/Decimal Points
          jimmyoneshot Level 1

          I've managed to find the solution for this - the currency formatter. If anyone's interested or has similar problems her's the code that solves it for me:-

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application
          
           xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
          
          <mx:Script>
          <![CDATA[
          
          
          import mx.formatters.NumberBaseRoundType;
          
          
          private function getTotal():void{
          
          var addon1Int:int = parseInt(addon1.text);
          
          var addon1Float:Number = parseFloat(addon1.text);
          
          var addon1Number:Number = new Number(addon1.text);
          
          
          var addon2Int:int = parseInt(addon2.text);
          
          var addon2Float:Number = parseFloat(addon2.text);
          
          var addon2Number:Number = new Number(addon2.text);
          
          
          var addon3Int:int = parseInt(addon3.text);
          
          var addon3Float:Number = parseFloat(addon3.text);
          
          var addon3Number:Number = new Number(addon3.text);
          
          
          var addon4Int:int = parseInt(addon4.text);
          
          var addon4Float:Number = parseFloat(addon4.text);
          
          var addon4Number:Number = new Number(addon4.text);
          
          
          var addon5Int:int = parseInt(addon5.text);
          
          var addon5Float:Number = parseFloat(addon5.text);
          
          var addon5Number:Number = new Number(addon5.text);
          
          
          var quantityNumber:Number = new Number(quantity.value); 
          
          
          var total:Number = (addon1Number + addon2Number + addon3Number + addon4Number + addon5Number) * (quantityNumber);
          totalAmount.text=currencyFormatter.format(total);
          }
          
          ]]>
          
          </mx:Script>
          
          
          <mx:CurrencyFormatter id="currencyFormatter" precision="2" rounding="none" decimalSeparatorTo="." thousandsSeparatorTo="," useThousandsSeparator="true" useNegativeSign="true" currencySymbol="£" alignSymbol="left"/>
          
          
          <mx:TextInput id="addon1" text="5.99"/>
          
          <mx:TextInput id="addon2" text="06.99"/>
          
          <mx:TextInput id="addon3" text="08.99"/>
          
          <mx:TextInput id="addon4" text="04.99"/>
          
          <mx:TextInput id="addon5" text="03.99"/>
          
          
          <mx:NumericStepper id="quantity"/>
          
          
          <mx:Button label="Button" click="getTotal();"/>
          
          
          <mx:Text id="totalAmount"/>
          
          </mx:Application>