Round up numbers

Round up numbers

andreas7ZYXQ
Advocate Advocate
4,314 Views
10 Replies
Message 1 of 11

Round up numbers

andreas7ZYXQ
Advocate
Advocate

Is it possible to round up numbers like..

2.1 = 3
2.5 = 3

I can only use normal math rules now. But im searching for a other way. Any ideas?


Thanks


0 Likes
Accepted solutions (1)
4,315 Views
10 Replies
Replies (10)
Message 2 of 11

ВeekeeCZ
Consultant
Consultant
Most simple is
(rtos (+ 2. 0.49) 2 0)
But if you actually DO a search, you'll find plenty of solutions.
0 Likes
Message 3 of 11

_gile
Consultant
Consultant
Accepted solution

Hi,

 

Here's a way:

 

;; round up
(defun ceil (x / n)
  (if (or (= (setq n (fix x)) x) (< x 0))
    n
    (1+ n)
  )
)

;; round down
(defun floor (x / n)
  (if (or (= (setq n (fix x)) x) (< 0 x))
    n
    (1- n)
  )
)


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 11

andreas7ZYXQ
Advocate
Advocate

Thanks!

Great solution

0 Likes
Message 5 of 11

john.uhden
Mentor
Mentor

0.49??  Is your inflation driving down the value of numbers?

Um, how about (fix (+ n 0.5))?

 

I've got something back home that can round to any precision, but I forget the code.

It might help if we had a reverse exponent function, as in (expt 10 n) = 100 and solve for n.

 

Maybe...

 

(defun revexpt (val n / exp)
  (setq exp 1)
  (while (/= val n)
    (setq exp (1+ exp))
    (setq val (/ val (expt n exp)))
  )
  (1+ exp)
)

 

No good... "divide by zero" sometimes

John F. Uhden

0 Likes
Message 6 of 11

_gile
Consultant
Consultant
;; round
(defun round (x)
  (fix ((if (minusp x) - +) x 0.5))
)

;; round to
(defun roundTo (num prec)
  (if (zerop (setq prec (abs prec)))
    num
    (* prec
       (fix ((if (minusp num)
               -
               +
             )
              (/ num prec)
              0.5
            )
       )
    )
  )
)

;; round up
(defun ceil (x / n)
  (if (or (= (setq n (fix x)) x) (< x 0))
    n
    (1+ n)
  )
)

;; round down
(defun floor (x / n)
  (if (or (= (setq n (fix x)) x) (< 0 x))
    n
    (1- n)
  )
)

_$ (roundTo 0.49 0.1)
0.5
_$ (round 0.49)
0
_$ (roundTo 0.49 0.1)
0.5
_$ (ceil 0.49)
1
_$ (floor 0.49)
0

 

_$ (round pi)
3
_$ (roundTo pi 0.01)
3.14
_$ (ceil pi)
4
_$ (floor pi)
3



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 11

john.uhden
Mentor
Mentor

minusp...  EXCELLENT!

John F. Uhden

0 Likes
Message 8 of 11

ВeekeeCZ
Consultant
Consultant

@john.uhden wrote:

0.49??  Is your inflation driving down the value of numbers?

...


I've meant (rtos (+ x 0.49) 2 0) for round up. Still most simple I think. 

But sure, it converts a result into string, so @_gile's suggestions are proper solutions.

 

0 Likes
Message 9 of 11

Kent1Cooper
Consultant
Consultant

@ВeekeeCZ wrote:
I've meant (rtos (+ x 0.49) 2 0) for round up. Still most simple I think. 

But sure, it converts a result into string, so @_gile's suggestions are proper solutions.


That depends on the precision of your number argument.  It wouldn't  round 6.009 up to 7.

 

But if the numbers are of no more than two decimal places and that's precise enough, you could, of course, wrap the whole thing in (atoi) to return an integer.

Kent Cooper, AIA
0 Likes
Message 10 of 11

john.uhden
Mentor
Mentor

Somewhere in my education I was taught to round even numbers down and odd numbers up.

2.5 => 2

3.5 => 4

 

I guess that would involve something like

(if (> (rem n 2) 1) => odd

John F. Uhden

0 Likes
Message 11 of 11

_gile
Consultant
Consultant

john.uhden a écrit :

Somewhere in my education I was taught to round even numbers down and odd numbers up.

2.5 => 2

3.5 => 4


It is just one convention among others.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub