Author Topic: Add zero in number  (Read 1578 times)

0 Members and 1 Guest are viewing this topic.

amc.dicsac

  • Newt
  • Posts: 109
  • Autocad 2008
Add zero in number
« 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
<a href="http:/http://axprogramlisp.blogspot.pe" class="bbc_link" target="_blank">By Alexander Castro</a>

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: Add zero in number
« Reply #1 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)
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Add zero in number
« Reply #2 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"

BIGAL

  • Swamp Rat
  • Posts: 1408
  • 40 + years of using Autocad
Re: Add zero in number
« Reply #3 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)))
    )
A man who never made a mistake never made anything

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Add zero in number
« Reply #4 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"
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: Add zero in number
« Reply #5 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
  )
)