Try This:
;|==============================================================================
Function Name: (pjk-leadzeros)
Arguments:
dig = integer; number of total digits represented in the string. must be
greater than zero.
str = string; a string representing a positive integer. must not have any
alpha characters or decimal points, minus sign, etc...
Usage: (pjk-leadzeros <Number of Digits> <String>)
Returns:
> The resulting string with leading zeros if the digits are greater than the
integer represented, the original string if the interger has equal or more
digits than <dig>. NIL if any argument is invalid
Description:
This function converts a string representing an integer number to a string
with the integer including leading zeros. This is extremely useful when used
within a sorted index to prevent single digit sorting common to windows (e.g.
1, 10, 11, 12..., 2, 20, 21, 22...)
================================================================================|;
(defun pjk-leadzeros (dig str)
(if (and dig str
(= (type dig) 'INT)
(= (type str) 'STR)
(> dig 0)
)
(repeat (- dig (strlen str))(setq str (strcat "0" str)))
(setq str nil)
)
str
) ;; End Function (pjk-leadzeros)