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 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!
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
set the_return to "000" & the_number as string
end if
if the_number is greater than 9 and the_number is less than 100 then
set the_return to "00" & the_number as string
end if
if the_number is greater than 99 and the_number is less than 1000 then
set the_return to "0" & the_number as string
end if
if the_number is greater than 999 then
set the_return to the_number
end if
if the_number is greater than 9999 then
set the_return to "9999" as string
end if
return the_return
end if
end UID_Convert
Thanks!
You could also try this way.
Zero_Pad(345, 8)
on Zero_Pad(This_Number, This_Pad)
repeat until (length of (This_Number as text)) = This_Pad
set This_Number to "0" & This_Number
end repeat
return This_Number
end Zero_Pad
Much nicer and cleaner than mine! Thanks! Here’s with a little bit of error checking:
Zero_Pad(1, 4)
on Zero_Pad(this_number, This_Pad)
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
end Zero_Pad
Many thanks!
my 2 ¢
Zero_Pad(1, 4)
on Zero_Pad(this_number, This_Pad)
if this_number > 10 ^ (This_Pad - 1) then return this_number as text
repeat This_Pad - (count this_number as string) times
set this_number to "0" & this_number
end repeat
return this_number
end Zero_Pad
Thanks for all the help guys!