# 3.5 数字类型转换

要将整数转换为浮点数，请使用函数float。

Function: float number 这将返回转换为浮点数的数字。如果number已经是浮点数，float则原样返回。

有四个函数可以将浮点数转换为整数；它们的不同之处在于它们如何取整。这些函数都接受参数 number和一个可选的参数 divisor。两个参数都可以是整数或浮点数。 除数也可能是 nil。如果除数是 nil或被省略，那这些函数会将数字转换为整数，如果它已经是整数，则原样返回。如果除数非nil，那么这些函数会将数字除以除数并将结果转换为整数。如果除数为零（无论是整数还是浮点数），Emacs 会抛出 arith-error 错误。

Function: truncate number \&optional divisor 这将返回number，通过向零取整将其转换为整数。

(truncate 1.2) ⇒ 1 (truncate 1.7) ⇒ 1 (truncate -1.2) ⇒ -1 (truncate -1.7) ⇒ -1 Function: floor number \&optional divisor 这将返回number，通过向下舍入（向负无穷大）转换为整数。

如果指定了除数，则使用去余，向下舍入。

(floor 1.2) ⇒ 1 (floor 1.7) ⇒ 1 (floor -1.2) ⇒ -2 (floor -1.7) ⇒ -2 (floor 5.99 3) ⇒ 1

Function: ceiling number \&optional divisor 这将返回number，通过向上舍入（向正无穷大）转换为整数。

(ceiling 1.2) ⇒ 2 (ceiling 1.7) ⇒ 2 (ceiling -1.2) ⇒ -1 (ceiling -1.7) ⇒ -1 Function: round number \&optional divisor 这将返回number，并向最接近的整数四舍五入转换为整数。如果在两个数正中间，则将其舍入偶数整数。

(round 1.2) ⇒ 1 (round 1.7) ⇒ 2 (round -1.2) ⇒ -1 (round -1.7) ⇒ -2


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://emacs-lisp.ivory.cafe/shu-zi/shu-zi-lei-xing-zhuan-huan.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
