メインコンテンツまでスキップ
に最終更新

ROUND

説明​

数値 x を指定した桁数に丸めます。

  • d を指定しない場合、x を最も近い整数に丸めます。
  • d が負の場合、小数点より左側の桁を d の絶対値に応じて丸めます。
  • x または d が null の場合、null を返します。
  • d が列で、最初の引数が Decimal 型の場合、結果の Decimal の小数桁数は入力の Decimal と同じになります。

別名​

  • DROUND

構文​

ROUND(<x> [ , <d> ])

パラメータ​

パラメータ説明
<x>丸める数値
<d>省略可能。丸める小数桁数

例​

select round(2.4);
+------------+
| round(2.4) |
+------------+
| 2 |
+------------+
select round(2.5);
+------------+
| round(2.5) |
+------------+
| 3 |
+------------+
select round(-3.4);
+-------------+
| round(-3.4) |
+-------------+
| -3 |
+-------------+
select round(-3.5);
+-------------+
| round(-3.5) |
+-------------+
| -4 |
+-------------+
select round(1667.2725, 2);
+---------------------+
| round(1667.2725, 2) |
+---------------------+
| 1667.27 |
+---------------------+
select round(1667.2725, -2);
+----------------------+
| round(1667.2725, -2) |
+----------------------+
| 1700 |
+----------------------+
CREATE TABLE test_enhanced_round (
rid int, flo float, dou double,
dec90 decimal(9, 0), dec91 decimal(9, 1), dec99 decimal(9, 9),
dec100 decimal(10,0), dec109 decimal(10,9), dec1010 decimal(10,10),
number int DEFAULT 1)
DISTRIBUTED BY HASH(rid)
PROPERTIES("replication_num" = "1" );

INSERT INTO test_enhanced_round
VALUES
(1, 12345.123, 123456789.123456789,
123456789, 12345678.1, 0.123456789,
123456789.1, 1.123456789, 0.123456789, 1);

SELECT number, dec90, round(dec90, number), dec91, round(dec91, number), dec99, round(dec99, number) FROM test_enhanced_round order by rid;
+--------+-----------+----------------------+------------+----------------------+-------------+----------------------+
| number | dec90 | round(dec90, number) | dec91 | round(dec91, number) | dec99 | round(dec99, number) |
+--------+-----------+----------------------+------------+----------------------+-------------+----------------------+
| 1 | 123456789 | 123456789 | 12345678.1 | 12345678.1 | 0.123456789 | 0.100000000 |
+--------+-----------+----------------------+------------+----------------------+-------------+----------------------+

使用上の注意​

2.5 は 3 に丸められます。2 に丸めたい場合は、round_bankers 関数を使用してください。