# force number to be X character digits?

Hi,

Is there a way to make it so that if x=7, I can display it as “007”, x=10 as “010”, x=100 as “100”

?

-patrick

Try something like this:

``````display dialog "Pad this number:" default answer "1"
set theNumber to (characters -3 through -1 of ("00" & (text returned of result))) as text

``````

thank you!!!

I’ve had to deal with this issue before, and I ended up with the code below. It gets the job done, but you will need to alter it if you would like more or less than 4 digits total. Hopefully you’ll see the logic and can figure out how to change the script based on the number of digits you need to zero-fill to. If it’s a low number of digits, it is a short script. But if it needed to be a whole lot of zeros, I’d look into some type of loop.

``````set num_string to 7

set num_string to num_string as string

set string_length to length of num_string

if string_length is 1 then -- if number is 1 digit long
set new_string to "000" & num_string -- add "000" to beginning of number string
else if string_length is 2 then -- if number is 2 digits long
set new_string to "00" & num_string
else if string_length is 3 then -- if number is 3 digits long
set new_string to "0" & num_string
else
set new_string to num_string
end if
``````

You could just change the line of code I posted:

``````set theNumber to (characters -4 through -1 of ("000" & (text returned of result))) as text

``````

If, for some reason, you wanted to use a handler, you could try something like this:

``````on zeroPadString(theNumber, desiredLength)
if (count theNumber) < desiredLength then
do shell script "jot -b 0 " & (desiredLength - 1)
return (characters (desiredLength - (desiredLength * 2)) through -1 of (((paragraphs of result) as text) & theNumber)) as text
else
return theNumber as text
end if

``````

Bruce’s solution is better (well, both work, but mine’s like an old Chevy). I didn’t see his response before posting mine. Thanks for the code Bruce.

Yeah… Thanks again Bruce! I modified your example and this is what I am using in my script and it works perfectly.

``````

set text item delimiters to ""
set maxnumber to totalcount as string
set zeroholder to ""
set zerochars to count characters of maxnumber
repeat with i from 1 to zerochars - 1
set zeroholder to zeroholder & "0"
end repeat
set theNumber to (characters -zerochars through -1 of (zeroholder & (uniqueid))) as text
return theNumber

``````
``````on padZeros(theNumber, zeroCount)
tell 10 ^ zeroCount to text 2 thru (zeroCount + 1) of (theNumber mod it + it as string)

--> "0007"

--> "0075"
``````

wow… that is very interesting… can you please break that down and explain what tell 10 ^ is actually doing? Thank you!

Nicely done, Qwerty.

Note that Qwerty’s code will shorten a number that is longer than “zeroCount.” This could be useful, but it can be prevented if you don’t want it to happen.

``````on padZeros(theNumber, zeroCount)
if (count (theNumber as text)) < zeroCount then
tell 10 ^ zeroCount to text 2 thru (zeroCount + 1) of (theNumber mod it + it as string)
else
return theNumber as text
end if

--> "0007"

--> "75000"

``````

Qwerty, is there any reason “(zeroCount + 1)” couldn’t be -1?

patrick, let’s step through Qwerty’s code using his first example (0007).

In this example, zeroCount is 4, so 10 ^ 4 is 10000. Thus, we are telling the number 10000 what to do.

Mod, for those who don’t know, divides two numbers and returns the remainder. This is the part that “shortens” numbers that are too long.

7 mod 10000 = 7
7 + 10000 = 10007

This effectively drops the first character, leaving you with the desired string.

I have no idea how item -1 works, but I am guessing that it would have to count the string first, to find out the number of the last character. Since zeroCount + 1 is exactly that, I thought it might be quicker. Comparing the two now, it doesn’t seem to make any visible difference.

Here is another version that doesn’t shorten it if it is bigger than zeroCount (structured slightly different than yours):

``````on padZeros(theNumber, zeroCount)
tell 10 ^ zeroCount to if theNumber is less than it then
text 2 thru -1 of (theNumber mod it + it as string)
else
theNumber as string
end if

``````on padZeros(theNumber, zeroCount)