FPTLS
FPTLS

Reputation: 197

Converting a real into an integer in lisp

How can I convert a real number to an integer in LISP?

Is there any primitive function?

Example:

3.0 => 3

Upvotes: 5

Views: 5478

Answers (2)

xabier
xabier

Reputation: 31

Other option is TRUNCATE. Examples

> (truncate 2.2)
=> 2
   0.20000005

> (truncate 2.9)
=> 2
   0.9000001

Upvotes: 3

Vatine
Vatine

Reputation: 21288

There are multiple ways.

I will be using f instead of a float number below.

If you're interested in the next-highest integer, (ceiling f) gives you that. If you are interested in the next-lowest integer, (floor f) gives you that (for values like 1.0, the two functions will return the same integer value). If you prefer having the closest integer, you can use (round f) to find it.

Those are the three simplest and most portable ways I can think of.

Upvotes: 7

Related Questions