round off

round off

Anonymous
Not applicable
974 Views
7 Replies
Message 1 of 8

round off

Anonymous
Not applicable

(defun c:BOP()
(defun _Text ( str)
(entmake
(list
'(000 . "TEXT")
(cons 007 (getvar 'textstyle))
(cons 001 str)
(cons 050 ang)
(cons 040 hgt)
(cons 010 tp)
(cons 011 tp)
'(072 . 1)
'(073 . 2)
(cons 210 ocs)
)
)
)


(setvar "osmode" 16383)
(setq osn (getvar "osmode"))
(setvar "osmode" osn)
(setq S1 (getdist "\nEnter Previous BOP:"))
(setq P (getdist "\nEnter Slope %:"))
(setq p1 (getpoint "\n Pipe Running Start Point: "))
(setq p2 (getpoint p1 "\n Pipe Running end Point: "))
(setq L (distance p1 p2))
(setq S2 (/ L P))
(setq BOP1 (- S1 S2))
(setvar "osmode" 0)
(setq tp (getpoint "\n Text Placing Point: "))
(setvar "osmode" osn)
(setq BOP2 (rtos BOP1 2 2))
(setq str (strcat "BOP " BOP2 " SOS"))
(setq hgt (getvar 'textsize)
ocs (trans '(0.0 0.0 1.0) 1 0 t)
ang (angle '(0.0 0.0) (trans (getvar 'ucsxdir) 0 ocs t))
)
(_Text str )
(princ)
)

 

 

i this programe i want to round off BOP1 into end 0 or 5

eg; 233.23(235)  232.1(230)  232.7(235)   

any one help me

0 Likes
975 Views
7 Replies
Replies (7)
Message 2 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

....

i this programe i want to round off BOP1 into end 0 or 5

eg; 233.23(235)  232.1(230)  232.7(235)   

....


Put "round" or "rounding" or "round off" into the Search window, and you will find many examples of how to do this, with varying degrees of explanation, rounding to the nearest integer value, or to the nearest multiple of some value such as your 5, some that round to the nearest value in whichever direction is closer, and some that only round up, or only round down, etc.

 

Yours is a simple-enough case -- here's one way to do it:

 

(defun round5 (num) (* 5 (fix (/ (+ num 2.5) 5))))

 

Command: (round5 232.1)
230

Command: (round5 233.23)
235

Command: (round5 232.7)
235

 

[EDIT:  That's if the number is positive, and for multiples of 5 only -- see @_gile's routine for one way to do it if it can be negative, and where the round-to-multiple-of number is not built in but must be specified as an argument.]

Kent Cooper, AIA
0 Likes
Message 3 of 8

_gile
Consultant
Consultant

Hi,

 

Try this:

(defun gc:round	(num prec)
  (if (zerop (setq prec (abs prec)))
    num
    (* prec (fix ((if (minusp num) - +) (/ num prec) 0.5)))
  )
)

(gc:round 233.25 5) => 235
(gc:round 232.1 5) => 230
(gc:round 232.7 5) => 235

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 8

Anonymous
Not applicable

OK GOOD THANKS

 

BUT I WANT TO ROUND THE DIGITAL NUMBERS UNDER THE VARIABLE (BOP1) IN M Y PROGRAM

THIS MY CALCULATED VALUE

I CHECKED IT MORE TIMES BUT I DIDN'T

0 Likes
Message 5 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

... I WANT TO ROUND THE DIGITAL NUMBERS UNDER THE VARIABLE (BOP1) ….


 

You can use a variable for an argument in a function like that.  Does this not work?

 

(gc:round BOP1 5)

Kent Cooper, AIA
0 Likes
Message 6 of 8

Anonymous
Not applicable

no

 

0 Likes
Message 7 of 8

Anonymous
Not applicable

any another method??

0 Likes
Message 8 of 8

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

no


 

Show us how you incorporated it into your code.

Kent Cooper, AIA
0 Likes