3 Replies Latest reply on Nov 25, 2010 12:30 PM by SpiritOfTheWinds

    Problem substracting 2 numbers

    SpiritOfTheWinds Level 1

      Okay, I've got a little problem with numbers in AS2.

       

      I'm taking 2 values from XML and converting them into Numbers with the properties as follow:

       

      function getPrixEmplacement():Number {
          return prixEmplacement;
      }
      function setPrixEmplacement(pPrixEmplacement:Number):Void {
          prixEmplacement = pPrixEmplacement;

      }
         
      function getEscompte():Number {
          return escompte;
      }
      function setEscompte(pEscompte:Number):Void {
          escompte = pEscompte;

      }

       

      So if I use getPrixEmplacement(), it should return a number, right?

       

      But in fact, it doesn't work...

       

      When I use

      var myString:String = getPrixEmplacement().toString();

      it returns "23000.0000", which it the value from XML.

      But if I use

      var myNumber:Number = getPrixEmplacement();

      it returns "NaN".

       

      var prix:String = getPrixEmplacement().toString(); // 23000.0000
      var prixStr:String = prix.substr(0, prix.length - 2); // 23000.00
      var escompte:String = getEscompte().toString(); // 4000.0000
      var escompteStr:String = escompte.substr(0, escompte.length - 2); // 4000.00

       

      // some of the tests I did (tried with replace "." by "," and many other things:

      var total:Number = Number(prixStr) - Number(escompteStr); // NaN

      var total:Number = getPrixEmplacement() - getEscompte(); // NaN

      var total:Number = Number(getPrixEmplacement()) - Number(getEscompte()); // NaN


      var totalStr:String = total.toString().substr(0, total.toString().length - 2); // N

       

      Why does it never returns a Number? Someone has some idea why it doesn't work? Is it because the number has too many decimals? Or is it the "." (dot)? I have to substract the 2 numbers so that 23000 - 4000 = 19000.

       

      Thanks!

        • 1. Re: Problem substracting 2 numbers
          Ned Murphy Adobe Community Professional & MVP

          Try converting it to a Number value in your function...

           

          function getPrixEmplacement():Number {
              return Number(prixEmplacement);
          }

           

          I don't think you need the function since you can convert a String to a number using the Number() function.   See how the two traced values differ with the following...

           

          var prixEmplacement = "23000.000";

           
          trace(prixEmplacement);

           
          trace(Number(prixEmplacement));

          1 person found this helpful
          • 2. Re: Problem substracting 2 numbers
            SpiritOfTheWinds Level 1

            I do need the function since it's in one of my .as classes. I just removed the class name in that function to simplify the code.

             

            I tried what you said, it doesn't work. I guess I'll try a work-around and transform my functions in String instead of numbers then only cast it as Numbers when needed to calculate.

            • 3. Re: Problem substracting 2 numbers
              SpiritOfTheWinds Level 1

              I finally found a working solution using parseFloat() instead of Number():

               

              var prixStr:String = getPrixEmplacement(); // getPrixEmplacement returns a String
                      var prix:Number = parseFloat(prixStr);
                      var escompteStr:String = getEscompte(); // getEscompte returns a String
                      var escompte:Number = parseFloat(escompteStr);
                      var total:Number = prix - escompte;
                      var totalStr = convertNumberToCurrencyString(total);

               

              function convertNumberToCurrencyString(total:Number):String {
                  var totalStr:String = new String(total.toString());
                  totalStr = totalStr.split(',').join('.');
                  var numbers:Array = totalStr.split(".");
                  var finalString:String = "";
                 
                  if (numbers.length > 1)
                  {
                      if (numbers[1].length == 0)
                          finalString = numbers[0].toString() + ".00";
                      else if (numbers[1].length == 1)
                          finalString = numbers[0].toString() + "." + numbers[1].toString() + "0";
                      else
                          finalString = numbers[0].toString() + "." + numbers[1].toString().substr(0, 2);
                  }
                  else
                  {
                      finalString = numbers[0].toString() + ".00";
                  }
                 
                  return finalString;
              }

               

              Since using a parse removes the decimals (0), I created a function that adds back the zeros since I need it as currency format. It works for:

              10000 returns 10000.00

              10000.1 returns 10000.10

              10000.12 returns 10000.12

              10000,12 returns 10000.12

              10000,129876 returns 10000.12