# How to round up numbers with lisp? need help ## Recommended Posts  I have a lisp code here that computes the plotting scale for a certain lot (surveying). My problem is that I don't know how to round up the values to the nearest hundreds or thousands to obtain the scale. For example, If my lisp computes the scale to be 45, It will show 100, when it computes 327, it must show 400, when it computes 923, it must show 1000, when 1134 it must show 1200. Can anyone help me with this? Here's the code by the way. THANK YOU GUYS! ```(defun c:ss ( / xn xs xx diff)
(setq xn (getpoint "\Pick the point on the extreme north of the lot boundary:"))
(setq xs (getpoint "\
Pick the point on the extreme south of the lot boundary:"))
(setq xx (getpoint "\Pick insertion point"))
(setq diff (/ (- (cadr xn) (cadr xs)) 0.3))
(command "text" "" "" diff)
(princ)
)

```

##### Share on other sites  .... If my lisp computes the scale to be 45, It will show 100, when it computes 327, it must show 400, when it computes 923, it must show 1000, when 1134 it must show 1200. Can anyone help me with this? Here's the

```[color="green"];positive only[/color]
(defun [color="blue"]round_100[/color] (x / a)
(if (zerop (setq x (fix (abs x)) a (rem x 100)))x(+ x(- 100 a))))
```

(mapcar 'round_100 '(45 327 923 1134))

;returns (100 400 1000 1200)

##### Share on other sites  @hanhphuc: Thanks for the code sir. But my problem is Im a newbie in lisp programming. I don't know how to merge or combine my code and yours. Can you post the final code sir? Thank you very much. More power.

regards

marko

##### Share on other sites  ...I don't know how to merge or combine my code and yours..

Just make sure sub-function is loaded,

or put it in the same lisp file ```(defun c:ss ( / xn xs xx diff)
[color="red"](if
(and [/color]
(setq xn (getpoint "[color="red"]\n[/color]Pick the point on the extreme north of the lot boundary:"))
(setq xs (getpoint xn "[color="red"]\n[/color]Pick the point on the extreme south of the lot boundary:"))
(setq xx (getpoint "[color="red"]\n[/color]Pick insertion point:"))
(setq diff (/ (- (cadr xn) (cadr xs)) 0.3))[color="red"])[/color]
(command "_text" [color="red"]xx[/color] "" "" ([color="blue"]round_100[/color] diff))
[color="red"])[/color]
(princ)
)
```

HTH

##### Share on other sites  Hate to throw a spanner in the works, but is that actually what you want to do?

The reason I ask is that there's a list of "standard" scales from which the actual scale of a drawing is selected. Working on the examples you cite I'd suggest the most appropriate scales would be 1:50, 1:500, 1:1000 and 1:1250, in that order.

Unfortunately I'm not good enough at lisp to "knock out" a function to achieve that in the way hanphuc did for you but I'd be happy to discuss it with you, and of course the list of scales.

dJE

##### Share on other sites  FWIW, Here's another way to write the rounding function:

```(defun roundup100 ( x )
(if (equal 0.0 (rem x 100) 1e-
(atoi (rtos x 2 0))
(* 100 (fix (1+ (/ x 100.0))))
)
)```

##### Share on other sites  @hanhphuc: Thank you so much Sir. The code worked perfectly. @Lee Mac (the legend): Thank you also sir, your code worked. @danellis: Yah, that's what i want to do. anyways, thank you sir.

Thank you all you guys. More power lisp masters.

##### Share on other sites  Guys, I need one more help. I was wrong about my statement at post #1. If the scale counts above 1000, it must be round up to the nearest thousand. for example. When it computes 1,100 ; 1234 ; 1023 ; this would all be equal to 2000. I have made the code but I don't know how to merge or combine it with the code for below 1000. I don't know how to use "if" or "cond". Thank you again sirs.

```(defun c:ss ( / xn xs xx diff)
(if
(and
(setq xn (getpoint "\nPick the point on the extreme north of the lot boundary:"))
(setq xs (getpoint xn "\nPick the point on the extreme south of the lot boundary:"))
(setq xx (getpoint "\nPick insertion point:"))
(setq diff (/ (- (cadr xn) (cadr xs)) 0.3)))
(command "_text" xx "" "" (round_100 diff))
)
(princ)
)

(defun round_100 (x / a)
(if (zerop (setq x (fix (abs x)) a (rem x 100)))x(+ x(- 100 a))))

;use this when diff is greater than 1000

(defun roundup1000 ( y )
(if (equal 0.0 (rem y 1000) 1e-
(atoi (rtos y 2 0))
(* 1000 (fix (1+ (/ y 1000.0))))
)
)``` ##### Share on other sites  Hello there Marko. If you are working with "LOTS", maybe I can help you. I am also working with "LOTS" these time.

If you are getting the plotting scale of a lot, why is it that the eastings were not included. I'll post a code. Hope it's close to what you want. ```(defun c:ss ( / xn xs xw xe diff miff)
(alert "1. Set the Plotting Scale by typing the command [ PLTSCALE ]
after getting the Plotting Scale.")
(defun roundup100 ( x )
(if (equal 0.0 (rem x 100) 1e-
(atoi (rtos x 2 0))
(* 100 (fix (1+ (/ x 100.0))))
)
)
(defun roundup1000 ( y )
(if (equal 0.0 (rem y 1000) 1e-
(atoi (rtos y 2 0))
(* 1000 (fix (1+ (/ y 1000.0))))
)
)
(setq xn (getpoint "\nPick the point on the extreme north of the lot boundary:"))
(setq xs (getpoint xn "\nPick the point on the extreme south of the lot boundary:"))
(setq xe (getpoint "\
Pick the point on the extreme east of the lot boundary:"))
(setq xw (getpoint "\
Pick the point on the extreme west of the lot boundary:"))
(setq diff (/ (- (cadr xn) (cadr xs)) 0.3))
(setq miff (/ (- (car xe) (car xw)) 0.3))
(if (< miff diff)
(progn
(if (<= diff 800)
(princ (strcat "\n\nPlotting Scale is 1: " (rtos (roundup100 diff) 2 0)))
)
(if (> diff 800)
(princ (strcat "\n\nPlotting Scale is 1: " (rtos (roundup1000 diff) 2 0)))
)
)
)
(if (> miff diff)
(progn
(if (<= miff 800)
(princ (strcat "\n\nPlotting Scale is 1: " (rtos (roundup100 miff) 2 0)))
)
(if (> miff 800)
(princ (strcat "\n\nPlotting Scale is 1: " (rtos (roundup1000 diff) 2 0)))
)
)
)
(princ)
)
```

Note: I've modified that code. From what I know, the plotting scales for lot are 100,200,300,400,500,600,700,800,1000,2000,3000,4000, and 5000. Or should I say there is no 1:900. If I'm wrong, just reply to this post and let's edit the code. cheers!

makimaki

## 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. Reply to this topic...

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.