Padding with Zeros

BlitzMax Forums/BlitzMax Beginners Area/Padding with Zeros

Eric(Posted 2005) [#1]
What is the simplist way to pad a number with leading zeros. The number will be 4 digits.

0001
0015
0256
0009

I know this is easy but for some reason it is eluding me.


Shambler(Posted 2005) [#2]
something like

Local a:Int=2
Local pad:string=a

While Len(pad$)<4
    pad$="0"+pad$
Wend

Print pad$

or this...

Local a:Int=2

Print Format(a,4)

Function Format:String(n:Int,p:Int)

Local pad$=n

While Len(pad$)<p
	pad$="0"+pad$
Wend

Return pad$

End Function



fredborg(Posted 2005) [#3]
For k = 0 To 9
	score = Rand(0,1111)
	Print Replace(RSet(score,4)," ","0")
Next



Eric(Posted 2005) [#4]
Thanks :)


JazzieB(Posted 2005) [#5]
If you know the maximum number of digits you need in a number then the following is enough (example for 8 digits)...

scoreText$=Right("0000000"+score,8)