round()
是一个系统函数,用于将提供的值舍入到最近的整数,或舍入到指定的小数位数。
round()
添加于 PostgreSQL 6.5。
用法
round()
有两种变体:一种是舍入到最近的整数,另一种是舍入到指定的小数位数。
整数舍入
round (numeric
) →numeric
round (double precision
) →double precision
对于 numeric
值,当出现小数位为 0.5 时,舍入规则是远离零。
对于 double precision
值,舍入规则是平台相关的,但“舍入到最近的偶数”是最常见的规则。
小数点舍入
round (numeric
,integer
) →integer
第一个值将被舍入到第二个值指定的精度。当出现小数位为 0.5 时,舍入规则是远离零。
变更历史
- PostgreSQL 6.5
- 添加于 (commit 0e9d75c6)
示例
round()
的基本用法示例
postgres=# SELECT round(3.14); round ------- 3 (1 row) postgres=# SELECT round(3.4999); round ------- 3 (1 row) postgres=# SELECT round(3.5); round ------- 4 (1 row) postgres=# SELECT round(3.94); round ------- 4 (1 row)
舍入到指定的小数位数
postgres=# SELECT round(3.1415, 2); round ------- 3.14 (1 row) postgres=# SELECT round(3.14159, 4); round -------- 3.1416 (1 row)
参考资料
- PostgreSQL 文档: 数学函数