/** * 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.
Flex has the NumberFormatter class...
ReplyDeleteThat's true. You can find it here:
ReplyDeletehttp://livedocs.adobe.com/flex/3/langref/mx/formatters/NumberFormatter.html
It doesn't allow you to pad the number with zeros though.
Maybe you should write
ReplyDelete(elemLen >= finalLength)
to prevent a no return of recursion if elemLen > finalLength due a missuse or error.
Thanks for the idea. I've updated the function to reflect the suggestion.
ReplyDeleteUsing recursion for a simple problem like adding zeroes is a bit over the top.
ReplyDeleteMaybe, but it's good to flex your recursion muscles every once in awhile.
ReplyDeletethanks for the code! :)
ReplyDeleteThank you for the code.
ReplyDeleteHow's this?
ReplyDeletefunction padString(string:String, padChar:String, finalLength:int, padLeft:Boolean = true):String {
while (string.length < finalLength) {
string = padLeft ? padChar + string : string + padChar;
}
return string;
}
@TharosTheDragon That works also, but it was partly an exercise in recursion.
ReplyDeleteHere's the version with types included:
ReplyDelete/**
* 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!