Padded Numbers

I’m trying to do a simple bit of math, but if my beginning number is padded with leading zeros, those zeros get stripped away. How do you keep that from happening?

Do you mean that your output is not what you desire? A few more details would be helpful. If you are looking for a way to preserve padded zeroes in a result, take a look at this thread. Although it covers a sequential numbering routine, you could easily modify it to protect leading zeroes in results.

Those leading zeros can only exist in text, while math requires their implicit or explicit coercion to numbers. I read your problem as “how do I get the leading zeros back again?”

Something like this?

set myList to {2, 3, 14, 5, 124} -- works only for positive numbers
set newList to {}
repeat with N in myList
	set end of newList to pad(N, 3)
end repeat
newList --> {"002", "003", "014", "005", "124"}

on pad(num, howLong)
	set thisNum to num as text
	set c to count thisNum
	repeat howLong - c times
		set thisNum to "0" & thisNum
	end repeat
	return thisNum as text
end pad

Would you mind hardcoding a number in your script?

-- The string is coerced to a number when doing the math
set test to "001" + 1

-- Add some zeroes to the front of the text, and then take the expected amount of text from the end.
set testA to text -3 thru -1 of ("00" & test) --> "002"