# Converting Numbers - 1 to 0001

Hi All,

this is probably really easy but how do you convert a single digit number into a 4 digit number?

ie, converting 1 so that it becomes 0001
or converting 20 so it becomes 0020
or 100 to become 0100, etc.

Many thanks!

• Mel

Hi Mel,

This should do it!

set number_list to {1, 20, 100, 9999}

repeat with this_number in number_list
set new_number to text -1 thru -4 of ("000" & this_number)
display dialog new_number
end repeat

Thanks,
Nik

Hi blend,

thanks for the reply, it didnâ€™t quite do what I needed but it put more on the right track and I created this function.

on UID_Convert(the_number)

if the_number is greater than 0 then

if the_number is less than 10 then
end if

if the_number is greater than 9 and the_number is less than 100 then
end if

if the_number is greater than 99 and the_number is less than 1000 then
end if

if the_number is greater than 999 then
end if

if the_number is greater than 9999 then
end if

return the_return

end if

end UID_Convert

Thanks!

• Mel

You could also try this way.

repeat until (length of (This_Number as text)) = This_Pad
set This_Number to "0" & This_Number
end repeat
return This_Number

Much nicer and cleaner than mine! Thanks! Hereâ€™s with a little bit of error checking:

if this_number is greater than 0 and this_number is less than 10000 then
repeat until (length of (this_number as text)) = This_Pad
set this_number to "0" & this_number
end repeat
return this_number
else
return 9999
end if

Many thanks!

• Mel

my 2 Â¢