6 Replies Latest reply on Aug 22, 2006 5:59 PM by NSurveyor

    Format a text field

    Pe_ka Level 1
      Hi,

      Anyone that happends to know if there is a good way of preformat a string for currency?

      Example;
      I have a number = "1234567" that i want to look like this "1.234.567" or similar and eventually include a currency symbol such as "$".

      I have checked the manual but can not find it.

      Cheers
        • 2. Re: Format a text field
          abeall Level 3
          There's no built in way. Here's a function I made awhile back:
          //v2
          strSeperator = function(num,delimiter){//takes long number(or string) and incrementally inserts a character
          num=num.toString();//convert Numbers to String
          pos=num.length;//var 'pos' is used to add a seperator at
          while(pos>3){//while loop, starts at String right moves to left
          pos-=3;//move left to next seperator point
          n2=num.slice(pos,num.length);//get all characters after seperator point as new substring
          n1=num.slice(0,pos);//get all characters before seperator point as new substring
          num=n1+delimiter+n2;//combine before-substring + dilimiter + after-substring as new 'num' String
          }
          return num;//return 'num' to where function is called
          }

          //example
          myNum = 7625428765237;
          myNumFormatted = strSeperator(myNum,".")
          trace(myNumFormatted);
          • 3. Re: Format a text field
            NSurveyor Level 2
            abeall, your code doesn't like numbers with decimals! It's a simple fix, just pull out the decimal part, do the stuff, then reattach the decimal part.
            • 4. Re: Format a text field
              Pe_ka Level 1
              quote:

              Originally posted by: NSurveyor
              abeall, your code doesn't like numbers with decimals! It's a simple fix, just pull out the decimal part, do the stuff, then reattach the decimal part.

              abeal or NSurveyor,

              could you please edit the code so it can handle decimals?

              Cheers

              • 5. Format a text field
                abeall Level 3
                Good call, NS!

                Here's my fix, Pe_ka:

                replace this:
                pos=num.length;

                with this:
                pos = (num.indexOf('.')==-1) ? num.length : num.indexOf('.');

                What that ugly line does is check to see if there's a "." - that's what the ? and : are for - in the number, if there is a '.' then it starts at the dot, otherwise it starts at the end of the number(actually at this point converted to string). Seems to work, the one time I tested it :-)
                • 6. Re: Format a text field
                  NSurveyor Level 2
                  One slight fix, for numbers like: -123345123, which gets formatted as: -,123,345,123 (because it is negative and the number of digits is a multiple of 3)

                  sign = num<0 ? '-' : '';
                  num=Math.abs(num).toString();
                  ...
                  return sign+num;