variable as 2 digit number

hey i have a little technical question
I would like to use a variable which is a number from 1 to 31 but i want it always to be a 2 digit number
eg 01,02,03,…,10,11
how do i solve this?

Just doodling. A simple way to extend the scope of the above could be…

set howLong to (button returned of ¬ 
(display dialog "Format?" buttons {"01", "001", "0001"}))'s length 
display dialog pad(text returned of ¬ 
(display dialog "Choose your poison" default answer ""), howLong) 
to pad(num, howLong) 
set zeros to "0" 
repeat howLong - 1 times 
set zeros to zeros & "0" 
end repeat 
return text -howLong thru -1 of (zeros & num) 
end pad

As far as I know “two digit numbers” < 10 do not exist. E.g. 02 = 2 where 2 is the symbolic representation of the number 2.
However, you could have “02” as text.
If that’s what you want, you could script:

repeat with k from 1 to 31
if k < 10 then
set k1 to "0" & k as text
else
set kl to k
end if
display dialog k1
end repeat

Eelco Houwink

This might work too:

set theNum to 2 
if (count of (theNum as text)) is 1 then 
set theNum to "0" & theNum 
display dialog theNum

Rob

Andreas posted an elegant solution to this not too long ago. I forgot exactly how it went, but was something using the last 2 items:

set num_list to {} 
repeat with n from 0 to 15 
 set end of num_list to text -2 thru -1 of ("0" & n) 
end repeat 
num_list
set x to 3
return text -1 thru -2 of ("0" & (x as text))
-- returns "03"
set x to 19
return text -1 thru -2 of ("0" & (x as text))
-- returns "19"

Obviously you would need to add appropriate error trapping…

display dialog pad(text returned of ¬ 
(display dialog "Choose your poison" default answer "")) 
to pad(num) 
return text -2 thru -1 of ("0" & num) 
end pad

As Kel has spotted, you don’t need the “as text” because the fact that the first item, the “0”, is text already forces the coercion.

I remember posting something like that and calling it “very elegant”. I put a lot of thought into producing it and eventually dragged it from the half of my brain that I call my MM half!