Apple Developer > Guide de script d'automatisation Mac contient une exemple comment ajouter des zéros à un nombre en AppleScript et en JavaScript :
Ajout de zéros non significatifs à un nombre
Les gestionnaires du Listing 20-11 et du Listing 20-12 convertissent un nombre en une chaîne de caractères et la précèdent de zéros en tête jusqu'à ce qu'elle atteigne une certaine longueur. Ils acceptent deux paramètres : le nombre auquel il faut ajouter des zéros non significatifs et le nombre maximal de zéros non significatifs à ajouter. Par exemple, si le nombre maximal de zéros non significatifs est fixé à 2, les résultats vont de 001 à 999. Si le nombre maximal de zéros non significatifs est de 3, les résultats vont de 0001 à 9999, et ainsi de suite.
Liste AppleScript :
on addLeadingZerosToNumber(theNumber, theMaxLeadingZeroCount)
-- Determine if the number is negative
set isNegative to theNumber is less than 0
-- Determine when the maximum number of digits will be reached
set theThreshold to (10 ^ theMaxLeadingZeroCount) as integer
-- If the number is shorter than the maximum number of digits
if theNumber is less than theThreshold then
-- If the number is negative, convert it to positive
if isNegative = true then set theNumber to -theNumber
-- Add the zeros to the number
set theLeadingZeros to ""
set theDigitCount to length of ((theNumber div 1) as string)
set theCharacterCount to (theMaxLeadingZeroCount + 1) - theDigitCount
repeat theCharacterCount times
set theLeadingZeros to (theLeadingZeros & "0") as string
end repeat
-- Make the number negative, if it was previously negative
if isNegative = true then set theLeadingZeros to "-" & theLeadingZeros
-- Return the prefixed number
return (theLeadingZeros & (theNumber as text)) as string
-- If the number is greater than or equal to the maximum number of digits
else
-- Return the original number
return theNumber as text
end if
end addLeadingZerosToNumber
Si vous préférez JavaScript, consultez le lien.