TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: deegeecees on January 26, 2006, 02:32:28 PM
-
Does anyone know of a better way to do this:
(setq ge_dim_scl1 240.00)
(setq ge_dim_scl1 (atoi (rtos ge_dim_scl1 2 2)))
return val = 240
-
Try
(setq ge_dim_scl1 240.00)
(fix ge_dim_scl1)
-
That gave me 239 when I tried it. Hmmm....
-
Command: (setq ge_dim_scl1 240.00)
240.0
Command: (fix ge_dim_scl1)
240
Maybe your value isn't really set to 240.00, it might be 239.99999999999999999999999999. Then fix will set it to the lower amount. I have seen rounding routines around (wow that was hard to type) but I don't have one.
Command: (fix 239.99999999)
239
-
Try this:
(defun ROUND (ZAHL STELLEN / TEMP )
(if (and(numberp ZAHL) (=(type STELLEN) 'INT))
(progn
(setq TEMP (abs ZAHL))
(repeat STELLEN (setq TEMP (* TEMP 10.0)))
(cond
( (> (- TEMP (fix TEMP)) 0.5) (setq TEMP (+ (fix TEMP) 1)))
( (< (- TEMP (fix TEMP)) 0.5) (setq TEMP (fix TEMP) ))
( (= (- TEMP (fix TEMP)) 0.5)
(if (equal (/ (fix TEMP) 2.0) (fix(/ (fix TEMP) 2.0)) 0.001)
(setq TEMP (fix TEMP) )
(setq TEMP (+ (fix TEMP) 1))
)
)
)
(repeat STELLEN (setq TEMP (/ TEMP 10.0)))
(setq TEMP (* TEMP (if (< Zahl 0 ) -1.0 1.0)))
)
)
TEMP
)
(round Pi 3) --> 3.142
(round 13.999 0) ->14.0
(fix(round 13.999 0)) -> 14
or this
(defun ROUND2 (ZAHL BASIS / TEMP )
(if (and(numberp ZAHL) (=(numberp BASIS))(> BASIS 0))
(progn
(setq TEMP (/ (abs ZAHL) BASIS))
(cond
( (> (- TEMP (fix TEMP)) 0.5) (setq TEMP (+ (fix TEMP) 1)))
( (< (- TEMP (fix TEMP)) 0.5) (setq TEMP (fix TEMP) ))
( (= (- TEMP (fix TEMP)) 0.5)
(if (equal (/ (fix TEMP) 2.0) (fix(/ (fix TEMP) 2.0)) 0.001)
(setq TEMP (fix TEMP) )
(setq TEMP (+ (fix TEMP) 1))
)
)
)
(setq TEMP (* BASIS TEMP (if (< Zahl 0 ) -1.0 1.0)))
)
)
(if (=(type BASIS)'INT) (fix TEMP) TEMP)
)
(ROUND2 12.63 0.5) ->12.5
(ROUND2 12.63 1) -> 13
(ROUND2 12.63 2) -> 12
-
Nice Contribution ! !
-
A couple others.
;; by Doug Broad
(defun round (value to)
(setq to (abs to))
(* to (fix (/ ((if (minusp value) - +) value (* to 0.5)) to)))
)
;; Joe Burke 2/23/03
(defun round (value to)
(if (zerop to)
value
(* (atoi (rtos (/ (float value) to) 2 0)) to)
)
)
-
And my two cent :-)
(defun rin (n)
(fix (distof (rtos n 2 0))))
(rin 8.4135480789);=>8
(rin 8.5135480789);=>9
~'J'~
-
;; CAB
; positive or negative numbers with variable break point
(defun rndup (num Brk)
(float (fix (+ num (* (/ num (abs num))(- 1 brk))))))
http://www.theswamp.org/forum/index.php?topic=3076.0
-
wow !
Speaking of skinning cats ;
hehehehehe
-
CADmium: Could you explain the logic of your functions line by line so I can understand them better?