Trying to get leading zeros for number lest than equal to 10

I’ve got this routine from someone on this forum. I thought I had it right but it is not the case.

I do not understand what is happening ? The purpose of the routine is to ensure I always have “Leading Zeroes”

The test seem to be faulty as it is not returning the right values.

If I get it right for number greater or equal to 10 I then do not get it OK for number less than 10.

If I change the algorithm and git it right for number that are less than 10 I do not get it right for numbers greater and equal to 10.

I do not know where to look for.

Thanks in advance!

set endStr to "00.PDF"

set newInitiatorName to "AAAA" & my twoDigitStr(theEventNumber) & endStr

on twoDigitStr(theNumber)
	if theNumber is less than 10 then
		return "0" & theNumber
		return theNumber as text
	end if
end twoDigitStr


best solution

on twoDigitStr(theNumber)
	return text -2 thru -1 of ("00" & theNumber)
end twoDigitStr


on pad(numText, howLong)
	set c to count numText
	repeat howLong - c times
		set numText to "0" & numText
	end repeat
	return numText
end pad

set PnumTextList to {pad("2", 2), pad("23", 4), pad("123", 2)} --> {"02", "0023", "123"}