TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on September 26, 2003, 02:40:55 PM
-
Mathematically speaking, does anyone know of a formula to do this:
given the number 502,345 -- I want in return 502,000
given the number 1,345,899 -- I want 1,346,000
given the number 18,373 -- I want 18,000
given the number 5389 -- I want 5000
given the number 130 -- I want 100
-
Not a formula .. and a bit crude, but will this do?
(defun round (a / b)
(cond ((>= a (setq b 1000.0)))
((>= a (setq b 100.0)))
((>= a (setq b 10.0)))
((< a 99) (setq b 1.0))
)
(* (fix (/ a b)) b)
)
-
Perfect, thanks Stig.
-
The only problem I see with it, is that it rounds it down, not up.
-
Duh! Didn't see that .. thanks Daron.
Even cruder (bet it can be shortened some):
(defun round (a / b)
(cond ((>= a (setq b 1000.0)))
((>= a (setq b 100.0)))
((>= a (setq b 10.0)))
((< a 99) (setq b 1.0))
)
(if (>= (- (abs (setq a (/ a b))) (fix (abs a))) 0.5)
(cond ((minusp a) (setq a (1- a)))
(T (setq a (1+ a))))
a
)
(* (fix a) b)
)
-
Stig, How did you get here?
Mark, Now we are sittin' at the cool table!
*Se7en does the "Saweeet Stig is here!" dance!*
-
Well, if ya don't get an invite ya just gotta crash in, right? :)
Followed Daron's link in the "previously frozen layer" thread on vbdesign.
Cool forum, by the way. Nice to see some people are still fond of AutoLISP :)
Should one register here?
-
(bet it can be shortened some)
sounds like a challenge to me <g>
-
...Should one register here?
Ooo yes please.
-
(- 8523 (rem 8523.00 1000))
something to think about..........
-
Thanks Se7en .. will do!
Yup, REM will do, too. I bet that code could be shortened to a couple of lines once someone's head's been wrapped around it (mine doesn't fit today).
-
how about this?
(defun rndx (x)
(cond
((>= x 1000) (* (atof (rtos (/ x 1000.0) 2 0)) 1000))
((>= x 100) (* (atof (rtos (/ x 100.0) 2 0)) 100))
((< x 100) (* (atof (rtos (/ x 10.0) 2 0)) 10))
)
)
-
Well, if ya don't get an invite ya just gotta crash in, right?
I want to invite everybody, but not sure about ethical limits. I get the feeling some boards don't like other boards coming in and posting a different site all over theirs. I figured by just linking a possible soultion to this site IS an invite for all who select the link to look around and hopefully register and become an active board member. :D BTW Stig, Welcome.
-
Daron, thank you.
Mark, interesting solution using string conversion. Using your idea, here's one that returns roundup to nearest ones, tens, hundreds etc..
(defun roundit (a)
(setq b 1.0)
(repeat (1- (strlen (rtos a 2 0)))(setq b (* b 10.0)))
(* (atof (rtos (/ a b) 2 0)) b)
)
^Doesn't accept negative numbers though!
-
This is why I want to round off to the nearest number.
http://theswamp.org/mark.lisp/files/ModelSpace-Grid/
Thanks for the help Stig.