Notifications
Clear all

[Closed] padding integers

Complete noob question here:

How do I print:

v = 3

as

“03”

without manually checking the length of the string and adding 0s to match.

I’m digging through the formattedprint function which on paper seems to be exactly what I want but I can’t get it to print 0s instead of spaces.

6 Replies

v = 3
print (substring ((v+100)as string) 2 2)
Since there is no way to store leading 0’s in a value you add 100 to it (103), convert to string (“103”), then use substring to extract the numbers you need (“03”).

-Eric

Haha! Neat!

Formattedprint I can get to print:

” 3″

or

” 3″

I suppose the other option would be to use that and then replace ” ” with “0”.

doesn’t this work?

formattedPrint 3 format:".2d" 

where the 2 is the total number of digits required, including leading zeros

Aha! Thank you! There’s the magic format string.

Nice drdubosc.

-Eric

…by the time you’ve unscrambled the docs in that little section, it does seem like magic. IMHO, the format string bit could do with a rewrite.