TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: amc.dicsac on November 08, 2019, 01:48:42 PM

Title: Add zero in number
Post 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
Title: Re: Add zero in number
Post by: PKENEWELL on November 08, 2019, 05:10:39 PM
Try This:

Code: [Select]
;|==============================================================================
  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)
Title: Re: Add zero in number
Post by: Lee Mac on November 08, 2019, 05:34:20 PM
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( s ) (substr (strcat "0" s) (min 2 (strlen s))))
Code - Auto/Visual Lisp: [Select]
  1. _$ (f "1")
  2. "01"
  3. _$ (f "10")
  4. "10"
  5. _$ (f "100")
  6. "100"
  7. _$ (f "1000")
  8. "1000"
Title: Re: Add zero in number
Post by: BIGAL on November 08, 2019, 08:25:34 PM
Another example we use D01, D10 etc

Code: [Select]
; 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)))
    )
Title: Re: Add zero in number
Post by: MP on November 08, 2019, 08:58:22 PM
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"
Title: Re: Add zero in number
Post by: Marc'Antonio Alessi on November 09, 2019, 03:50:27 AM
Another for > 10
Code: [Select]
; (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
  )
)