What would you use to zero pad a number in Flex/AS3?

lpfavreau picture lpfavreau · Mar 20, 2009 · Viewed 17.2k times · Source

Duplicate of this one.

What would you use to pad zeroes to the left of a number in Flex/AS3?

Is there an equivalent to printf or NumberFormat that does this?

I'm looking for the nicest implementation of this or something similar:

public function zeroPad(number:int, width:int):String {
    // number = 46, width = 4 would return "0046"
}

Answer

Phil picture Phil · Mar 21, 2009
public function zeroPad(number:int, width:int):String {
   var ret:String = ""+number;
   while( ret.length < width )
       ret="0" + ret;
   return ret;
}