4 Replies Latest reply on Apr 20, 2006 11:55 PM by dasura

    formatting numbers 1000->1.000

    dasura
      Hi everyone!

      Im doing a calculator and I want the numbers readable. Instead of 100000 I want 100,000. I found another post on this forum with the following enclosed code. However the code doesnt work...could someone take a look? Thank you all!!

      function formatNum(someNumber:Number):String {
      // formats a number
      var money:String = someNumber.toString();
      var myArray:Array = new Array(Math.ceil(money.length/3));
      for (var i = 0; i<myArray.length; i++) {
      var s = money.length-((i+1)*3);
      var e = money.length-(i*3);
      if (s<0) {
      s = 0;
      }
      myArray = money.slice(s, e);
      }
      myArray.reverse();
      var newVal:String = "";
      for (var j = 0; j<myArray.length; j++) {
      newVal += myArray[j];
      if (j != myArray.length-1) {
      newVal += ",";
      }
      }
      var formatedPrice = newVal;
      return formatedPrice;
      }
      var someNumber:Number = 30000;
      formatNum(someNumber);
      // returns 30,000