Leading Zero on Single Digits (Flash)

I am using Flash CS3 - AS 3.0.

I have the following code that I use to make the number of images external: I'm not sure that you are all familiar with Slideshow pro (I don't think you need to help me).

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

How do I make the leading zero appearing opposite the number that appears in this text box as long as it is 9 and under?

I hope my question does not confuse.

+3
source share
6 answers
 function leadingZero(num : Number) : String {
    if(num < 10) {
       return "0" + num;
    }
    return num.toString();
 }
+3
source

To further optimize Seanonymous's 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 be noticeable only when repeating hundreds of lines / numbers.

+4
source

, , (, 0001 0991 ..)

1: -, 0

var leadingZeroes: String = "000000000";

2:

var intToString: String = String (myInteger);

3:

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

4: ( )

TxtField.text = (leadingZeroes + intToString);

Voila!

+1

- , , :

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

:

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

timeText: 03: 08.0042

+1
0

:

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

, Array join() , while.

this

however, that one heading has a “ruby” in it when we talk about as3. I suggest making this one of the duplicates of this.

0
source

Source: https://habr.com/ru/post/1718114/


All Articles