Add Zeros to Numbers less than 10 with ActionScript 3
04/11/2008
Many times we need when we use numbers or dates in counters such as the games, to handle numbers with zeros:
for example:
For this, i developed a simple class called AddZero with two methods:
-addZeroToNumber to handle Numbers.
-addZeroToString to handle Strings.
The use of my class is basic and simple:
import phoxer.Strings.AddZero; AddZero.addZeroToNumber(7); // 07 AddZero.addZeroToNumber(10); // 10; AddZero.addZeroToNumber(6.5); // 06.5; AddZero.addZeroToString("7"); // 07 AddZero.addZeroToString("10"); // 10;
My Class AddZero:
/** by .:[PHOXER]:. http://www.phoxer.com v 1.8; */ package phoxer.Strings{ public class AddZero{ public static function addZeroToNumber(nm:Number):String{ var nst:String=""; if(nm<10){ nst+=String("0"+nm); }else{ nst=String(nm); } return nst; } public static function addZeroToString(st:String):String{ var num:Number=Number(st); var nst:String=""; if(num<10){ nst+=String("0"+num); }else{ nst=String(num); } return nst; } } }
The class seems useless at first view but this saves us (at least to me) a lot of time, for example when we want to show dates and a format like 02/09/08, without this kind show 2/9/8.
