Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

Flash now has the NumberFormatter() class as part of flash.globalization.*

it will format numbers in the users locale

http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/globalization/NumberFormatter.html

you can also set the number of leading, or trailing zeros

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top