How to convert a float to integer?[Solved]

Discussion of Common Lisp
Post Reply
BreakDS
Posts: 3
Joined: Sat Aug 21, 2010 7:02 pm

How to convert a float to integer?[Solved]

Post by BreakDS » Sat Aug 21, 2010 7:10 pm

I thought the function 'round' might work, but it fails to do what I want.
Neither does 'coerce'. So I'm wondering is there any built-in function that works on this, say convert 2.1 to 2 and 2.9 to 3?

Thanks for answering a newbie's question.
Last edited by BreakDS on Mon Aug 23, 2010 6:21 pm, edited 1 time in total.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to convert a float to integer?

Post by ramarren » Sat Aug 21, 2010 10:37 pm

Common Lisp has a large number of rounding functions for many rounding modes. What do you mean ROUND did not work?

Code: Select all

CL-USER> (round 2.1)
2
0.099999905
CL-USER> (round 2.9)
3
-0.099999905
Do note that the second printed number is the secondary return value of the function, the remainder, and it is discarded in standard evaluation context.

BreakDS
Posts: 3
Joined: Sat Aug 21, 2010 7:02 pm

Re: How to convert a float to integer?

Post by BreakDS » Sun Aug 22, 2010 7:58 pm

Thx. I tried
(setf a (round 2.1))
and it works.
I was just confused by the TWO return value.
What should I do, if I want to capture these 2 value in 2 different variable by using "round"?

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: How to convert a float to integer?

Post by ramarren » Sun Aug 22, 2010 11:17 pm


BreakDS
Posts: 3
Joined: Sat Aug 21, 2010 7:02 pm

Re: How to convert a float to integer?

Post by BreakDS » Mon Aug 23, 2010 6:21 pm

That's great. Thanks!
Ramarren wrote:There is a chapter on multiple value returning functions in Practical Common Lisp.

Post Reply