1 Reply Latest reply on Jun 20, 2007 12:51 PM by kglad

    Currency Cure

    stratcat9
      I have four input boxes that output a calculated total and the final total needs to be displayed as currency. I found the formatAsDollars function which looks like it does exaclty what I'm looking for. Now, how or where do I insert that "total" into the formatAsDollars function to get that result? My syntax savvy is not too good. I saw a lot of comments about the formatAsDollars function but no real clear answer as to how you run your dang variable through there! Not sure how to get it correctly within the Event handler as I've been notified many times. An example would be nice. Anyway, here's the code:

      on (release, keyPress "<Enter>") {
      total = parseFloat(loads)*parseFloat(barrels)*parseFloat(price)*parseFloat(error)*52;
      message = total

      function formatAsDollars (amount ):String {

      // return a 0 dollar value if amount is not valid
      // (you may optionally want to return an empty string)
      if (isNaN(amount)) {
      return "$0.00";
      }

      // round the amount to the nearest 100th
      amount = Math.round(amount*100)/100;

      // convert the number to a string
      var amount_str:String = String(amount);

      // split the string by the decimal point, separating the
      // whole dollar value from the cents. Dollars are in
      // amount_array[0], cents in amount_array[1]
      var amount_array = amount_str.split(".");

      // if there are no cents, add them using "00"
      if (amount_array[1] == undefined) {
      amount_array[1] = "00";
      }

      // if the cents are too short, add necessary "0"
      if (amount_array[1].length == 1) {
      amount_array[1] += "0";
      }

      // add the dollars portion of the amount to an
      // array in sections of 3 to separate with commas
      var dollar_array:Array = new Array();
      var start:Number;
      var end:Number = amount_array[0].length;
      while (end > 0) {
      start = Math.max(end - 3, 0);
      dollar_array.unshift(amount_array[0].slice(start, end));
      end = start;
      }

      // assign dollar value back in amount_array with
      // the a comma delimited value from dollar_array
      amount_array[0] = dollar_array.join(",");

      // finally construct the return string joining
      // dollars with cents in amount_array
      return ("$" + amount_array.join("."));
      }

      Thanks for any help