Jump to content

Lisp to round text (with numbers in them) to the nearest 0.05


Recommended Posts

Posted

Hi all,

 

First post, was hoping to get some help.

 

I need a lisp that will round a number in autocad to the nearest 0.05, I have a numerous drawings with bench levels for lots (civil), I would like to be able to swipe multiple pieces of text and round them all to nearest 0.05.

 

Any help with this would be much appreciated

 

Cheers

Posted

I've seen many versions of this over the years, but this was the first one I saw and it still works.

 ;;; Written by Doug Broad
;;; If value given 'to' argument is a real, a real is returned.
;;; Rounds to nearest multiple of 'to' real or integer.

(defun round (value to / try)
 (setq to (abs to))
 (* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))
)

; examples:
; (round 12232e-015 1)
; 0
; (round 12232e-015 1.0)
; 0.0
; (round 123.6 1.0)
; 124.0
; (round -123.6 1.0)
; -124.0
; (round 6.00000000008 4)
; 8
; 6.00000000008  rounded to the nearest multiple of 4 is 8 

Posted

Just be careful if your text is formatted MText, since for such cases the text content will contain numerical formatting codes.

Posted

Hi guys,

 

thanks for the help.

 

I changed it to this

 

;;; Written by Doug Broad

;;; If value given 'to' argument is a real, a real is returned.

;;; Rounds to nearest multiple of 'to' real or integer.

 

(defun c:bench (value to / try)

(setq to (abs to))

(* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))

)

 

; examples:

; (round 12232e-015 1)

; 0

; (round 12232e-015 1.0)

; 0.0

; (round 123.6 1.0)

; 124.0

; (round -123.6 1.0)

; -124.0

; (round 6.00000000008 4)

; 8

; 6.00000000008 rounded to the nearest multiple of 4 is 8

 

when I type bench into the command line I get this message '; error: too few arguments'

 

Not sure what im doing wrong. I have no real experience writing lisp code. Have I missed something?

 

Thanks again for your help

Posted

You need to use the original code, look at the examples. To round 1.2345 to the nearest 0.05 enter (round 1.2345 0.05)

It's set up this way so you can easily call it from another routine using the "value" you want to round and the "to" you want it rounded to.

A command function that starts with "c:" cannot have the "value" and "to" arguments. For a command function you would have to add prompts for and save those values.

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...