Naju
Naju

Reputation: 1541

Three digit Number formatter

I want to display numbers in 3 digits (001-999). I don't known how to format this number.. I can easily format the decimal numbers, but can't do this.

Upvotes: 0

Views: 1570

Answers (2)

Mark Knol
Mark Knol

Reputation: 10163

You can use NumberUtils.format or StringUtils.padLeft to format a Number using the Temple Library. The utils are located inside the utils module.

using StringUtils.padLeft

var myValue:int = 3;
trace(StringUtils.padLeft(String(myValue), 3, '0'));
// outputs 003

using NumberUtils.format

var myValue:int = 3;
trace(NumberUtils.format(myValue, ',', '.', 0, 3));
// outputs 003

Upvotes: 3

ndm
ndm

Reputation: 60463

That's pretty easy... convert your Number into a String, check its length, and if less than 3, prepend zeros.

Example:

var num:Number = 1;
var numStr:String = num.toString();

while(numStr.length < 3)
{
    numStr = '0' + numStr;
}

trace(numStr); // outputs 001

Upvotes: 4

Related Questions