views:

1437

answers:

1

I have always used the NumberFormat class in Java to do simple number padding ie. turn 1, 2, 3... into 0001, 0002, 0003....

Is there a similar utility in ActionScript to do my padding, or will I have to write a custom function?

+3  A: 

Seems there is nothing built in. This will do it:

function padZero (num:Number, digits:int):String {
  var ret:String = num.toString();
  while (ret.length < digits)
    ret = "0" + ret;
  return ret;
}

Though com.adobe.utils.NumberFormatter has addLeadingZero(n:Number):String, which sounds promising, but according to the spec it pads a single zero to numbers between -1 and 10. I guess the function is useful for time output only.

Tomalak