TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: amc.dicsac on November 08, 2019, 01:48:42 PM
-
Hello
I need your help please, I want to add a zero to the numbers from 1 to 9 without the numbers from 10 being modified
Example:
1 --> 01
2 --> 02
10 ---> 10
1000 ---> 1000
-
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)
-
Another -
_$ (f "1")
"01"
_$ (f "10")
"10"
_$ (f "100")
"100"
_$ (f "1000")
"1000"
-
Another example we use D01, D10 etc
; if less than 10
(if (< (car dwgnum) 10.0)
(setq newstr2 (strcat dwgname "-D0" (rtos sheetnum 2 0)))
(setq newstr2 (strcat dwgname "-D" (rtos sheetnum 2 0)))
)
-
Another for fun:
(defun p (x) (strcat (if (< (strlen (setq x (vl-princ-to-string x))) 2) "0" "") x))
(p 1) >> "01"
(p "2") >> "02"
(p 10) >> "10"
(p "1000") >> "1000"
-
Another for > 10
; (ALE_String_ZeroPad "1" 2) => "01"
; (ALE_String_ZeroPad "1" 5) => "00001"
; (ALE_String_ZeroPad "Pippo" 2) => "Pippo"
; Original By RRB - Robert Bell
;
(defun ALE_String_ZeroPad (In_Str DgtNum / ZerLst)
(strcat
(apply 'strcat
(repeat (- DgtNum (strlen In_Str))
(setq ZerLst (cons "0" ZerLst))
)
)
In_Str
)
)