Google analytics code

Friday, September 18, 2009

Flash / ActionScript: String padding (leading zero)

I was working on a date/time format in Flash and needed a way to pad values out so they always took up the same amount of space. For example making sure a month always has two digits. Making sure it displays a "05" instead of "5". I couldn't find anything built into AS2 or AS3 to do this so I wrote something that would. It allows you to pass in a value and pad either the left or right side up to a given amount of characters.

/**
* This function will pad the left or right side of any variable passed in
* elem [AS object]
* padChar: String
* finalLength: Number
* dir: String
*
* return String
*/
function padValue(elem, padChar, finalLength, dir)
{
  //make sure the direction is in lowercase
  dir = dir.toLowerCase();

  //store the elem length
  var elemLen = elem.toString().length;

  //check the length for escape clause
  if(elemLen >= finalLength)
  {
    return elem;
  }

  //pad the value
  switch(dir)
  {
    default:
    case 'l':
      return padValue(padChar + elem, padChar, finalLength, dir);
      break;
    case 'r':
      return padValue(elem + padChar, padChar, finalLength, dir);
      break;
  }
}


This function accepts an object that can be converted to a string with the toString method, the character you want to use for padding, the final length of the string and wither you want it padded on the left or right side using the character "l" or "r".

For example if you have a number you want padded with three 0 so the final outcome will look like "0005" you would call the function like this.
var exampleNumber:Number = 5;
padValue(exampleNumber, "0", 4, 'l');


If you want to use this in FLEX you might want to type the parameters on the function and give it a return value.

11 comments:

  1. Flex has the NumberFormatter class...

    ReplyDelete
  2. That's true. You can find it here:
    http://livedocs.adobe.com/flex/3/langref/mx/formatters/NumberFormatter.html

    It doesn't allow you to pad the number with zeros though.

    ReplyDelete
  3. Maybe you should write
    (elemLen >= finalLength)
    to prevent a no return of recursion if elemLen > finalLength due a missuse or error.

    ReplyDelete
  4. Thanks for the idea. I've updated the function to reflect the suggestion.

    ReplyDelete
  5. Using recursion for a simple problem like adding zeroes is a bit over the top.

    ReplyDelete
  6. Maybe, but it's good to flex your recursion muscles every once in awhile.

    ReplyDelete
  7. thanks for the code! :)

    ReplyDelete
  8. How's this?

    function padString(string:String, padChar:String, finalLength:int, padLeft:Boolean = true):String {

    while (string.length < finalLength) {

    string = padLeft ? padChar + string : string + padChar;

    }

    return string;

    }

    ReplyDelete
  9. @TharosTheDragon That works also, but it was partly an exercise in recursion.

    ReplyDelete
  10. Here's the version with types included:

    /**
    * This function will pad the left or right side of any variable passed in
    * elem [AS object]
    * padChar: String
    * finalLength: Number
    * dir: String
    *
    * return String
    */
    public function padValue(elem : Object, padChar :String , finalLength : Number, dir: String) : String
    {
    //make sure the direction is in lowercase
    dir = dir.toLowerCase();

    //store the elem length
    var elemLen : uint = elem.toString().length;

    //check the length for escape clause
    if(elemLen >= finalLength)
    {
    return elem.toString();
    }

    //pad the value
    switch(dir)
    {
    default:
    case 'l':
    return padValue(padChar + elem, padChar, finalLength, dir);
    break;
    case 'r':
    return padValue(elem + padChar, padChar, finalLength, dir);
    break;
    }
    }

    Thanks for the snippet!

    ReplyDelete

If you found this page useful, or you have any feedback, please leave a comment.