Be a Supporter!

number format 0000 in AS2

  • 714 Views
  • 3 Replies
New Topic Respond to this Topic
YoinK
YoinK
  • Member since: Jan. 5, 2001
  • Offline.
Forum Stats
Member
Level 60
Game Developer
number format 0000 in AS2 2011-11-26 17:45:38 Reply

I want to be able to format a variable to display it with a specific amount of digits even if it is 0.
so if there are 4 digits i want it to look like 0000. and if it goes up in number... then it's 0001 .. 0002.. 0003.. etc etc etc.


Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!

4urentertainment
4urentertainment
  • Member since: Aug. 1, 2008
  • Offline.
Forum Stats
Moderator
Level 13
Game Developer
Response to number format 0000 in AS2 2011-11-26 18:13:05 Reply

This will need some tricky string manipulation. I've just given this a few minutes of thought, and here's what I came up with.

You'd first want to convert your number to a string, to find out how many digits it has. Then add to this string the remainder from 4 - number of characters in the string. Add 0's that is. I could give you some code in AS3, but since you want it in AS2, you'll have to search around a bit. But that's basically the pseudo code right there.

radthemad4
radthemad4
  • Member since: Jan. 7, 2009
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
Response to number format 0000 in AS2 2011-11-26 18:24:55 Reply

Try this:

var number:Number=12 //The number you want to display
var output:String=""; // A string that holds the eventual display thingy
var desiredDigits:Number=5; //The number of digits you want

digits=Math.floor(Math.log(number)/Math.log(10))+1;

for(var i:Number=0;i<desiredDigits-digits;i++){
	output+="0"; //Adding 0's to it
}

output+=String(number); //Adding the number itself

trace(output);
YoinK
YoinK
  • Member since: Jan. 5, 2001
  • Offline.
Forum Stats
Member
Level 60
Game Developer
Response to number format 0000 in AS2 2011-11-26 19:52:37 Reply

At 11/26/11 06:24 PM, radthemad4 wrote: Try this:

Perfect! that works perfectly! thank you very much!


Kanye West Did What??????
They should make a black guy the manager, they know how to run shit!
2 Girls 1 Cup .... THE SONG!!!!