PDA

View Full Version : padding integers


thatoneguy
01-15-2008, 10:29 PM
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.

PiXeL_MoNKeY
01-15-2008, 10:58 PM
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

thatoneguy
01-15-2008, 11:11 PM
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".

drdubosc
01-15-2008, 11:32 PM
doesn't this work?
formattedPrint 3 format:".2d"
where the 2 is the total number of digits required, including leading zeros

thatoneguy
01-16-2008, 12:04 AM
Aha! Thank you! There's the magic format string.

PiXeL_MoNKeY
01-16-2008, 12:31 AM
Nice drdubosc.

-Eric

drdubosc
01-16-2008, 10:10 AM
....the magic format string.

:) ...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.

CGTalk Moderation
01-16-2008, 10:10 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.