GLBasic forum

Codesnippets => Code Snippets => Topic started by: spicypixel on 2011-May-08

Title: Pad an Integer with Leading Zeros (For High Scores)
Post by: spicypixel on 2011-May-08
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 =)
Title: Re: Pad an Integer with Leading Zeros (For High Scores)
Post by: ampos on 2011-May-08
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)
Title: Re: Pad an Integer with Leading Zeros (For High Scores)
Post by: spicypixel on 2011-May-08
Useful ;)
Title: Re: Pad an Integer with Leading Zeros (For High Scores)
Post by: Moebius on 2011-May-08
Definitely useful  :good:
Just make sure you LOCAL text$ explicitly - anything not localled is implicitly made GLOBAL in GLB.