Code Red > AutoLISP (Vanilla / Visual)
math formula
Mark:
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
smadsen:
Not a formula .. and a bit crude, but will this do?
--- Code: ---(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)
)
--- End code ---
Mark:
Perfect, thanks Stig.
daron:
The only problem I see with it, is that it rounds it down, not up.
smadsen:
Duh! Didn't see that .. thanks Daron.
Even cruder (bet it can be shortened some):
--- Code: ---(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)
)
--- End code ---
Navigation
[0] Message Index
[#] Next page
Go to full version