Code Red > AutoLISP (Vanilla / Visual)
Add zero in number
amc.dicsac:
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
PKENEWELL:
Try This:
--- Code: ---;|==============================================================================
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)
--- End code ---
Lee Mac:
Another -
--- Code - Auto/Visual Lisp: ---(defun f ( s ) (substr (strcat "0" s) (min 2 (strlen s))))
--- Code - Auto/Visual Lisp: ---_$ (f "1")"01"_$ (f "10")"10"_$ (f "100")"100"_$ (f "1000")"1000"
BIGAL:
Another example we use D01, D10 etc
--- Code: ---; 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)))
)
--- End code ---
MP:
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"
Navigation
[0] Message Index
[#] Next page
Go to full version