/**
* 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.