• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML [Solved] Zero Padding a number as a string

C

Cupid Stunt

Guest
I'm creating a clock for my game and I'm having issues with zero padding the numbers.
I could use if statements and I will if necessary, but that's ugly code.
In python I'd simply use:
Code:
 '{:02d}:{:02d}'.format(minutes, seconds)
My output would be: 01:24 or 03:07 or 12:05
The point is to add the extra zero into the string when it's needed to make the number always have 2 digits.

My current code is:
Code:
 draw_text(20,20, "Time: " + string(minutes) + ":" + string(seconds));
This works well, but does not ensure that each number is 2 digits.
 

Yal

šŸ§ *penguin noises*
GMC Elder
I use this script for it:
Code:
///string_fix(number,places)
//Returns a zero-padded (or cut) string of the number so that it gets desired number of places.
var del;
argument0 = string(floor(argument0))
del       = string_length(argument0) - argument1
if(del>0){
    return string_delete(argument0,1,del)
}
else if(del<0){
    return string_repeat("0",-del) + argument0
}
else{
    return argument0
}
If the number is longer than the desired number of decimal places, the most significant digits are just omitted. You might wanna change that if this isn't what you want. (Remove the string_delete line and just return argument0 unchanged instead)
 

chamaeleon

Member
I'm creating a clock for my game and I'm having issues with zero padding the numbers.
I could use if statements and I will if necessary, but that's ugly code.
In python I'd simply use:
Code:
 '{:02d}:{:02d}'.format(minutes, seconds)
My output would be: 01:24 or 03:07 or 12:05
The point is to add the extra zero into the string when it's needed to make the number always have 2 digits.

My current code is:
Code:
 draw_text(20,20, "Time: " + string(minutes) + ":" + string(seconds));
This works well, but does not ensure that each number is 2 digits.
Code:
var s = string_replace_all(string_format(minutes, 2, 0) + ":" + string_format(seconds, 2, 0), " ", "0");
draw_text(20, 20, s);
Just realize GMS is rather weak when it comes to string processing. You'll have to do an awful lot yourself (unless you happen to find someone else's code or an extension).
 
C

Cupid Stunt

Guest
Code:
var s = string_replace_all(string_format(minutes, 2, 0) + ":" + string_format(seconds, 2, 0), " ", "0");
draw_text(20, 20, s);
Just realize GMS is rather weak when it comes to string processing. You'll have to do an awful lot yourself (unless you happen to find someone else's code or an extension).
Indeed, but your answer does the job. Fortunately this cumbersome code only runs once per second.
string_format does not do the job completely.
I could make a script with what you've shown but if I needed to use it that much I'm probably working on a spreadsheet and thus in the wrong language.

For my game your solution is best. Thanks a bunch
Though I did make mine into a single line to avoid the load/store:
Code:
draw_text(20,20, "Time: " + string_replace_all(string_format(minutes, 2, 0) + ":" + string_format(seconds, 2, 0), " ", "0"));
 
Top