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

前端 未结 9 1087
春和景丽
春和景丽 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.

提交回复
热议问题