From: gtasso on
Hi all,
My name is George Erick Tasso I am new to lisp as well as this list.
Could anyone kindly tell me how to round a number to any number of
decimal places ?

5.123452 - > round to 2 decimal place would be 5.12

many thanks

From: Carl Taylor on

<gtasso(a)rbv.gov.vu> wrote in message
news:1165364837.032657.48280(a)80g2000cwy.googlegroups.com...
> Hi all,
> My name is George Erick Tasso I am new to lisp as well as this list.
> Could anyone kindly tell me how to round a number to any number of
> decimal places ?
>
> 5.123452 - > round to 2 decimal place would be 5.12

Lisp is alive and well in Vanuatu!

Here are two functions to ponder.

Carl Taylor


CL-USER 1 >
(defun round-float (f d)
(nth-value 0
(read-from-string
(format nil "~25,V,,,F" d f))))
ROUND-FLOAT

CL-USER 2 > (round-float 5.123452 2)
5.12

Or another way with some argument edit checks,

CL-USER 3 >
(defun roundup (inx n)
(assert (typep inx 'real) (inx)
"First argument <~S> s/b a real number." inx)
(assert (typep n '(integer 0 14)) (n)
"Second argument <~S> s/b an integer in {0..14}." n)
(let* ((m-factor (expt 10 n))
(a-factor (float (/ 5 (expt 10 (1+ n)))))
(result (* (signum inx)
(/ (truncate (* m-factor (+ a-factor (abs
inx))))
m-factor)))
(truncated-result (truncate result)))
(if (= result truncated-result)
truncated-result
result)))
ROUNDUP

CL-USER 4 > (compile *)
ROUNDUP

CL-USER 5 > (roundup 5.123452 2)
5.12


From: gtasso on

Carl Taylor wrote:
> Lisp is alive and well in Vanuatu!

I have checked around it seemed as if i am the only one doing LISP in
the whole country :)

>
> Here are two functions to ponder.
>
> Carl Taylor
>
>
> CL-USER 1 >
> (defun round-float (f d)
> (nth-value 0
> (read-from-string
> (format nil "~25,V,,,F" d f))))
> ROUND-FLOAT

I implemented this in my program as its easy at this stage to wrap my
head around.

Thanks


George E Tasso

From: Barry Margolin on
In article <qJodh.155344$Fi1.26724(a)bgtnsc05-news.ops.worldnet.att.net>,
"Carl Taylor" <carltaylor(a)att.net> wrote:

> (defun roundup (inx n)
> (assert (typep inx 'real) (inx)
> "First argument <~S> s/b a real number." inx)
> (assert (typep n '(integer 0 14)) (n)
> "Second argument <~S> s/b an integer in {0..14}." n)
> (let* ((m-factor (expt 10 n))
> (a-factor (float (/ 5 (expt 10 (1+ n)))))
> (result (* (signum inx)
> (/ (truncate (* m-factor (+ a-factor (abs
> inx))))
> m-factor)))
> (truncated-result (truncate result)))
> (if (= result truncated-result)
> truncated-result
> result)))

Is there a reason you add A-FACTOR and then use TRUNCATE, rather than
just using ROUND in the first place?

--
Barry Margolin, barmar(a)alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Andreas Thiele on

<gtasso(a)rbv.gov.vu> schrieb im Newsbeitrag news:1165368549.958171.310040(a)l12g2000cwl.googlegroups.com...
>
> Carl Taylor wrote:
>> Lisp is alive and well in Vanuatu!
>
> I have checked around it seemed as if i am the only one doing LISP in
> the whole country :)

Sometimes I think I'm one of the very few on c.l.l all over the world, but I can reassure you the dark figure is high :))

Andreas