• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Legacy GM Add zeros to strings

M

molcap

Guest
hello
I'm creating a score system and I'm trying to put '0' before the score, for example:

actual score: 2300
score shown : 0002300

I know that is possible with 'string' functions without using 'if score>999 then "000"+string(score)' but I can't remember how

Can you help me?

Thanks
 
B

Boogery Boogers

Guest
Yes, you can use string_format to add Spaces before the numbers, then use string_replace to replace the Spaces with Zeros.

Code:
score_show=string_format(score_actual,7,0);
score_show=string_replace(score_show,' ','0')
hmmmm...I just tried this piece of code now and it's displaying something like "0 2300".
 
L

Laurent57

Guest
score_show=string(score_actual);
l=string_length(score_show);
d=7-l;
score_show=d*"0"+score_show;
 
B

Boogery Boogers

Guest
Thanks for the reply, but I'm more curious about why that code isn't doing "0002300", while others seem to have found it helpful.
 
J

JFitch

Guest
string_replace only replaces the first time it appears. That's why it was only replacing the first space with a 0. You need string_replace_all.
Code:
score_show=string_format(score_actual,7,0);
score_show=string_replace_all(score_show,' ','0')
 
B

Boogery Boogers

Guest
string_replace only replaces the first time it appears. That's why it was only replacing the first space with a 0. You need string_replace_all.
Code:
score_show=string_format(score_actual,7,0);
score_show=string_replace_all(score_show,' ','0')
Maaaaaan, I totally missed that function in the manual. Many thanks for pointing out and explaining this. It's exactly what I needed to know.
 
Top