Jump to content

Leading zero's in attribute value


Recommended Posts

Posted
On 7/9/2022 at 5:46 PM, 3dwannab said:

 

I hope you don't mind Alan but I've add support for minus values here also based on your function here and trimmed any existing padding (leading 0's) so this will work for any text that already has padding applied.

 

(defun AT:NumFix (numInput lenNew / lenInput minus result)
  (vl-load-com)
  ; Fix number string with leading zeros
  ; numInput - Number string to fix
  ; lenNew - Number of characters for final string
  ; Alan J. Thompson, 10.29.09. https://www.cadtutor.net/forum/topic/14276-alanjts-misc-useful-lisp-subroutines/?do=findComment&comment=151562
  ; 3dwannab 2022.07.09. Added minus values support
  (setq minus (vl-string-search "-" numInput))
  (setq numInput (vl-string-left-trim "0" numInput)) ;; Trims existing padding
  (setq lenInput (strlen numInput))
  ;; If a minus is found set the lenInput minus 1 and trim the minus. This will get added back later on
  (if minus 
    (progn 
      (setq lenInput (abs (- 1 (strlen numInput))))
      (setq numInput (vl-string-left-trim "-" numInput))
    )
  )
  (setq result (vl-princ-to-string numInput))
  (while (< (strlen result) lenNew)
    (setq result (strcat "0" result))
  ) ;_ while
  ;; Output the result depending on whether there's a minus or not
  (if minus 
    (strcat "-" result)
    result
  )
)
(princ (AT:NumFix "-003" 6)) ; -> -000003
(princ "\n")
(princ (AT:NumFix "003" 12)) ; -> 000000000003
(princ "\n")
(princ (AT:NumFix "00" 4)) ; -> 0000

 

 

 

Have at it. Hope it helped. However, you need not credit me on this as the only thing you used of mine was my function name. lol

 

I thought I'd post a couple examples of your desired result. 

Both are recursive, but the second uses list over string manipulation as it's just faster (not that it's going to make much of a difference on something like this.

 

(defun _numPad (str len)
  ((lambda (foo)
     (if (wcmatch str "-*")
       (strcat "-" (foo (substr str 2) len))
       (foo str len)
     )
   )
    (lambda (n l)
      (if (< (strlen n) l)
        (foo (strcat "0" n) l)
        n
      )
    )
  )
)


(defun _numPad2 (str len)
  ((lambda (foo lst)
     (vl-list->string (if (eq (car lst) 45)
                        (cons 45 (foo (cdr lst) len))
                        (foo lst len)
                      )
     )
   )
    (lambda (x l)
      (if (< (length x) l)
        (foo (cons 48 x) l)
        x
      )
    )
    (vl-string->list str)
  )
)

 

  • Like 1
  • Thanks 1
Posted

Thanks Alan. Always good to see how it should be done.

  • 2 weeks later...
Posted
On 8/15/2022 at 6:09 PM, 3dwannab said:

Thanks Alan. Always good to see how it should be done.

*Should* is questionable. Just another way to go about things. 

  • Funny 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...