Zero filled string (useful for score table)

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
ChangeV
Posts: 29
Joined: Mar 19, 2006 4:07
Location: Southern Mississippi

Zero filled string (useful for score table)

Postby ChangeV » Sep 03, 2007 17:29

Here is string converting routine that will fill the value's left side with zero then trim by number of digits.

The result is similar to RSET sub routine, but it's filled with zero from the left.
It should be useful for displaying score in games.

This routine is for positive number only. (it's much simpler)

I also added negative value supported version at the end.(it's little bit complex than positive-only version)


Code: Select all

' Convert positive number into zero-filled string.
'
' Great for displaying score table in the game.
' Can be used with DRAW_STRING routine too.
'
' *Be sure to use enough digits so result won't look ugly.

' USAGE : ZeroFill ( variable_name, number_of_digits )




#define ZeroFill(VARNAME, NUMDIGIT) (right(string(NUMDIGIT - 1, "0" )& VARNAME, NUMDIGIT))





'--- example ---

DIM AS INTEGER score

score = 12345 'some positive value

SCREEN 18, 32

print "zero filled string"
print
print "Great for displaying score or score table in the game."
print "Can be used with DRAW_STRING routine too."
print
print
print "positive value test with "; score
print "show only 3 digits from right", "'" + ZeroFill(score, 3) + "'"
print "show only 8 digits from right", "'" + ZeroFill(score, 8) + "'"

'--- draw string example ---
DRAW STRING (10, 200), "High Score table", RGB(255, 255, 255)
DRAW STRING (12, 212), "  1st " + ZeroFill(10000, 5), RGB(255, 215, 255)
DRAW STRING (14, 224), "  2nd " + ZeroFill(8000, 5), RGB(255, 175, 255)
DRAW STRING (16, 236), "  3rd " + ZeroFill(6000, 5), RGB(255, 135, 255)
DRAW STRING (14, 248), "  4th " + ZeroFill(4000, 5), RGB(255, 95, 255)
DRAW STRING (12, 260), "  5th " + ZeroFill(2000, 5), RGB(255, 55, 255)

SLEEP
END





'----------------------------------------------------------------------------
'--- negative value supported version ---
'There are various style to display negative value.
'Here is one of the style.
'It will add 'minus' symbol at the front if negative value is used.
'Then trim the string so, it will fit into the 'number of digits'
'
'for example, -123 with 6 digits will be '-00123' (- symbol is included in the number of digits)
'
'
'#define ZEROFILL(VARNAME, NUMDIGIT) (RIGHT("-" + right(string(NUMDIGIT - 1, "0" ) & ABS(VARNAME), NUMDIGIT - iif(VARNAME < 0,1,0)), NUMDIGIT + iif(VARNAME < 0,1,0)))
Lachie Dazdarian
Posts: 2338
Joined: May 31, 2005 9:59
Location: Croatia
Contact:

Postby Lachie Dazdarian » Sep 03, 2007 21:45

Heh, I was using a silly hack for this so I guess I don't have to anymore. Thanks!
dani.user
Posts: 284
Joined: Sep 30, 2006 10:41

Postby dani.user » Sep 04, 2007 15:32

Why not use the format function?
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Sep 04, 2007 23:50

dani.user wrote:Why not use the format function?


Like to show us an example?
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Postby anonymous1337 » Sep 04, 2007 23:56

@notthecheatr:

Code: Select all

#include "vbcompat.bi"

print format(10000000, "00000000000000000")
sleep
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Sep 05, 2007 0:13

anonymous1337 wrote:@notthecheatr:

Code: Select all

#include "vbcompat.bi"

print format(10000000, "00000000000000000")
sleep


Thanks... I was too lazy to check it myself XD
ChangeV
Posts: 29
Joined: Mar 19, 2006 4:07
Location: Southern Mississippi

Postby ChangeV » Sep 05, 2007 3:44

Hehe, I expected that.

FORMAT is very useful and flexible...
But it ignores trimming if there aren't enough zeroes.
Result can go over the display area.

anyway, I can use RIGHT function to trim the result.

Code: Select all

RIGHT(FORMAT(score, "00000000"), 8)


I just wanted to share some old technique.
Such as this
PRINT RIGHT$("0000000" + LTRIM$(STR$(SCORE)), 8)

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests