Pad an Integer with Leading Zeros (For High Scores)

Previous topic - Next topic

spicypixel

This is a nice little function to use if you wish to print scores, timers, highscores etc. but require to use a fixed number of characters so the numbers don't jump about as they increase and decrease in value.

Function
Code (glbasic) Select

// .------------------------------------.
// |  Pad INTEGER with leading zeros    |
// `------------------------------------'
FUNCTION PrintPaddedNumber: num, zeros, numx, numy
text$ = FORMAT$(zeros, 0, num)
text$ = REPLACE$(text$," ","0")
PRINT text$, numx, numy
ENDFUNCTION


Usage
Code (glbasic) Select
PrintPaddedNumber(20000, 6, 5, 5)

This will print the number 020000 in the top left of the screen at co-ordinates 5,5. Obviously using 6 for the zeros parameter only gives a range of 000000...999999, alternatively if the zeros parameter was 3 we have a range of 000...999 handy for a timer :)

Again I hope you guys find it useful =)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

ampos

It is easier something like

Code (glbasic) Select
PRINT right$("0000000000"+score,lenght),x,y

(this code can be used as GLBasic do not add a space (" ") before the number when converting it to a string, as most languages do)
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

spicypixel

http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

Moebius

Definitely useful  :good:
Just make sure you LOCAL text$ explicitly - anything not localled is implicitly made GLOBAL in GLB.
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary