Ruby-like Question: Make this function shorter (ActionScript 3)

前端 未结 9 1085
春和景丽
春和景丽 2021-01-14 01:38

I just wrote this incredibly verbose code to turn numbers like 2 into 02. Can you make this function shorter, please (maintaning the functionality)?

9条回答
  •  礼貌的吻别
    2021-01-14 01:52

    I've always done this by taking a string that is the maximum padded width of zeros containing all zeros, then appeneding the string to padded to the end of the zeros string and then using substring to get the right hand Length digits.

    Something like:

    function pad(num:int, length:unit):String{
        var big_padded:String "0000000000000000000000000000" + num.toString();
        return big_padded.substring(big_padded.length - length);
     }
    

提交回复
热议问题