3

I am using Flash CS3 - AS 3.0.

I have the following code that I am using to make the image count external: I am not sure if you all are familiar with Slideshow pro (I dont think that you need to in order to help me).

function albumStuff(event:SSPDataEvent) {
     if (event.type=="albumData") {
     total1.text =  event.data.totalImages;
     }
}

How would I make a leading zero come up infront of the number that comes up in that text field as long as it's 9 and under?

I hope my question isn't confusing.

6 Answers 6

4
 function leadingZero(num : Number) : String {
    if(num < 10) {
       return "0" + num;
    }
    return num.toString();
 }
3
  • thanks, but how would I incorporate this into the code i currently have :/ i just started doing 3.0 , cause i had to do it for this client
    – Shonna
    Sep 20, 2009 at 17:59
  • 1
    total1.text = leadingZero(Number(event.data.totalImages));
    – maxmc
    Sep 20, 2009 at 21:23
  • I greatly dislike this answer. What if num was -10? That is "less than 10" and would get a zero appended...
    – 1owk3y
    Oct 5, 2016 at 3:02
4

To further optimize Seanonymous' answer:

function addLeadingZero(val:Number, places:uint):String
{
    var result:String = val.toString();
    for(var i:int = result.length; i < places; i++)
    {
        result = '0' + result;
    }
    return result;
}

Speed will only noticeable when iterating hundreds of strings/numbers.

1

Here's a better method that will work if you have more than one decimal place (so something like 0001 to 0991 etc.)

Step 1: create a placeholder variable that has the max number of 0's

var leadingZeroes:String="000000000";

Step 2: turn your number into a string

var intToString:String=String(myInteger);

Step 3: shave off the end of your placeholder based on the number of characters in your number

leadingZeroes=leadingZeroes.substr(0,leadingZeroes.length-intToString.length);

Step 4: print the concatenated string to your textfield (or use it wherever)

TxtField.text=String(leadingZeroes+intToString);

Voila!

1

Adding one zero to numbers below ten is all well & good, but if you want to be a bit more thorough this will allow you to add any number of zeros:

function alz( value:uint, places:Number ):String { //Add Leading Zeros
    var result:String = value.toString();
    while ( result.length < places ) {
        result = '0' + result;
    }
    return result;
}

Then you can use the same function for varying numbers of leading zeros:

var timeText:String = alz( minutes, 2) + ":" + 
                        alz( seconds, 2 ) + "." + 
                        alz( milliseconds, 4 );

timeText: 03:08.0042

0

You may use Casalib's NumberUtil.

The library's website.

For a new comer, the library provides a lot of useful little functions and it is a very good learning material when you look at its source.

0
0

Here is a one-liner way:

private function leadingZeros(value:int, numDigits:int):String
{
    return String(new Array(numDigits + 1).join("0") + String(value)).substr(-numDigits, numDigits);
}

though, I'm not certain if the Array's join() method is faster than the while loop.

this thread is a duplicate of this

however, that one's title has "ruby" in it, when we're talking about as3. i suggesting making that one a duplicate of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.