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

前端 未结 9 1086
春和景丽
春和景丽 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:43

    How about this:

    public static function format(n:int, len:int):String {
      var v:String = n.toString();
      return (v.length >= len) ? v : String(Math.pow(10, len) + n).substr(1);
    }
    

    There is not built-in function to do this btw. If you need decent padding functions, take a look at the StringUtils in Apache Commons Lang.

    0 讨论(0)
  • 2021-01-14 01:46

    Props to dirkgently and all others who have responded here, but apparently people are voting up without actually trying the code.

    dirkgently's final function is mostly correct, but his '>' needs to be a '<'.

    This function performs as desired (tested fairly thoroughly):

    public static function format(n:int, minimumLength:int):String {
      var v:String = n.toString();
      var stillNeed:int = minimumLength - v.length;
      return (stillNeed < 0) ? v : String(Math.pow(10, stillNeed) + v).substr(1);
    }
    
    0 讨论(0)
  • 2021-01-14 01:48

    The as3corelib package put out by adobe has a nice little NumberFormatter class that uses a series of STATIC classes. In this case you could use the addLeadingZero function.

    //The following method is from the NumberFormatter class of the as3corelib package by Adobe.
    public static function addLeadingZero(n:Number):String
    {
    var out:String = String(n);
    
    if(n < 10 && n > -1)
    {
        out = "0" + out;
    }
    
    return out;
    }
    

    I included the function just to show it's simplicity, but I would use the package instead of yoinking the function because it provides many other useful features like StringUtils, encryption methods like MD5, blowfish, etc.

    You can download the package here For newer users you must provide a classpath to where this package lives. It is also smart to import the classes instead of using their fully qualified class names.

    0 讨论(0)
  • 2021-01-14 01:48
    private function leadingZeros(value:int, numDigits:int):String
    {
                return String(new Array(numDigits + 1).join("0") + String(value)).substr(-numDigits, numDigits);
    }
    
    0 讨论(0)
  • 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);
     }
    
    0 讨论(0)
  • 2021-01-14 01:57

    Christophe Herreman almost got it right, but his method adds more zeroes and not the differential amount. I fixed it a bit:

    public static function format(n:int, minimumLength:int):String {
      var v:String = n.toString();
      var stillNeed:int = minimumLength - v.length;       
      return (stillNeed > 0) ? v : String(Math.pow(10, stillNeed) + v).substr(1);
    }
    

    My earlier try:

     public static function format(n:int, minimumLength:int):String {
        var stillNeed:int = minimumLength - n.toString().length;               
        return (n.split("").reverse().join("") as int) // 32 -> 23
                 *Math.pow(10, stillNeed > 0 ? stillNeed : 0).toString() // 23000
                     .split("").reverse().join("");  // 00032
     }
    
     public static function formatAny(n:Number, minimumLength:int):String {
        return format((int)n) + n.toString().split('.')[ 1 ];
     }
    
     // use this if you want to handle -ve numbers as well
     public static function formatAny(n:Number, minimumLength:int):String {
        return (n < 0 ? '-' : '') + formatAny(n, minimumLength);
     }
    
    0 讨论(0)
提交回复
热议问题