5 Replies Latest reply on Apr 5, 2007 1:25 PM by kglad

    calculation question

    swodiddy
      Can someone take a look at this and tell me why I get "NaN" for:

      combinedLabel.text = Number(gratuity.text) + Number(contribLabel.text);

      here is the entire function:

      private function calculatesemi():Void
      {
      var hipercent = (.06);
      var contribpercent:Number = Number(increase.text);
      if (contribpercent >= (.07)) {
      contribpercent = hipercent;
      }
      var totalcontrib:Number = Number(subtotal.text) * contribpercent / (24);
      var totalcombined:Number = totalcontrib * .50;
      var atip:Number = totalcombined;
      if (isNaN(atip)) {
      contribLabel.text = "";
      } else {
      contribLabel.text = formatCurrency(atip);
      }
      var percent:Number = Number(increase.text);
      var totalinvested:Number = Number(subtotal.text) * percent;
      var totalsemi:Number = totalinvested / 24;
      var dtip:Number = totalsemi;
      if (isNaN(dtip)) {
      gratuity.text = "";
      } else {
      gratuity.text = formatCurrency(dtip);

      combinedLabel.text = Number(gratuity.text) + Number(contribLabel.text);
      }
      }
        • 1. Re: calculation question
          kglad Adobe Community Professional & MVP
          either gratuity.text or contribLabel.text (or both) cannot be resolved to a number.
          • 2. Re: calculation question
            swodiddy Level 1
            Is it because of the formatCurrency function being put on the fields prior to me calculating the combined total? Do you have any suggestions on how I can get around this? BTW, you helped me tremendously on my last problem/post; and I thank you again for offering your time. That is too cool that you offer your expertise for folks you don't even know!
            • 3. Re: calculation question
              kglad Adobe Community Professional & MVP
              if formatCurrency() inserts a $ or Euro sign, then yes, that would prevent Number() from being able to resolve the returned string to a number.

              in that situation, either don't call formatCurrency() until all calculations are complete or maintain two sets of variables: one set that contains the numbers you need for calculations and one set that you use for currency display.
              • 4. Re: calculation question
                swodiddy Level 1
                Wow! Without even looking at the other code. You are sharp my friend!! That's what it was:

                private function formatCurrency(value:Number):String
                {
                var cents:Number = Math.round(value*100);
                var result:String = "$" + Math.floor(cents/100) + ".";
                cents %= 100;
                if (cents < 10) {
                result += "0";
                }
                result += String(cents);
                return result;
                }
                • 5. Re: calculation question
                  kglad Adobe Community Professional & MVP
                  you're welcome.