ROUND

将值四舍五入到指定的小数位数,保留原始精度和小数位数。大于或等于 0.5 的小数部分将四舍五入。小于 0.5 的分数向下舍入(截断)。

行为类型

不可变

语法

ROUND ( expression [ , places ] )

参数

expression
NUMERICDOUBLE PRECISION (FLOAT) 类型的表达式。
places
INTEGER 值。当 places 为正整数时,Vertica 使用指定位数对小数点右侧的值进行四舍五入。当 places 为负整数时,Vertica 使用指定位数对小数点左侧的值进行四舍五入。

注意

ROUNDNUMERIC 数据类型配合使用,则返回 NUMERIC 并保留原始精度和标度。

=> SELECT ROUND(3.5);
 ROUND
-------
   4.0
(1 row)

示例

=> SELECT ROUND(2.0, 1.0) FROM dual;
 ROUND
-------
   2.0
(1 row)
=> SELECT ROUND(12.345, 2.0);
 ROUND
--------
 12.350
(1 row)
=> SELECT ROUND(3.444444444444444);
       ROUND
-------------------
 3.000000000000000
(1 row)
=> SELECT ROUND(3.14159, 3);
  ROUND
---------
 3.14200
(1 row)
=> SELECT ROUND(1234567, -3);
  ROUND
---------
 1235000
(1 row)
=> SELECT ROUND(3.4999, -1);
 ROUND
--------
 0.0000
(1 row)

以下示例创建了一个包含两列的表,添加了一行值,显示小数点左侧和右侧的舍入示例。

=> CREATE TABLE sampleround (roundcol1 NUMERIC, roundcol2 NUMERIC);
CREATE TABLE

=> INSERT INTO sampleround VALUES (1234567, .1234567);
 OUTPUT
--------
      1
(1 row)

=> SELECT ROUND(roundcol1,-3) AS pn3, ROUND(roundcol1,-4) AS pn4, ROUND(roundcol1,-5) AS pn5 FROM sampleround;

           pn3           |           pn4           |           pn5
-------------------------+-------------------------+-------------------------
 1235000.000000000000000 | 1230000.000000000000000 | 1200000.000000000000000
(1 row)

=> SELECT ROUND(roundcol2,3) AS p3, ROUND(roundcol2,4) AS p4, ROUND(roundcol2,5) AS p5 FROM sampleround;

        p3         |        p4         |        p5
-------------------+-------------------+-------------------
 0.123000000000000 | 0.123500000000000 | 0.123460000000000
(1 row)