Christophe Herreman几乎做对了,但他的方法增加了更多的零,而不是微分量。我修复了一点:
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);
}
我之前的尝试:
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);
}