ROUND() 関数は、数値フィールドの小数点以下の桁数を、指定した長さに丸めるために使用します。
注: 多くのデータベースシステムは、予期するようなものとは異なった丸めを行います。
数値の小数部分を整数へ丸めるときは、.1 ~ .4 の場合は小さい方の整数に、.5 ~ .9 の場合は大きい方の整数になると教えらてきました。
小数以下の数 1 ~ 9 が同じように出現しても、.5 を常に切り上げると無限大方向に多少のバイアスがかかります。
(訳注:以下の文は意味不明なので訳せません:)Many database systems have adopted the IEEE 754 standard for
arithmetic operations, according to which the default rounding behavior is
"round half to even." In this scheme, .5 is rounded to the nearest even integer.
So, both 11.5 and 12.5 would be rounded to 12.
(訳注:銀行丸めについて述べています。銀行丸めとは、「端数が 0.5 より小さいなら切捨て、端数が 0.5 より大きいならば切上げ
、端数がちょうど 0.5 だった時は、結果が偶数になる方へ丸める」ことです。例えば、11.5(切上げ) と 12.5(切捨て) は両方ともに丸めの結果は 12 となります。
SELECT ROUND(column_name,decimals) FROM table_name;
パラメータ | 説明 |
---|---|
column_name | 必須。丸めるフィールド |
decimals | 必須。小数点以下の桁数を指定 |
このチュートリアルでは、よく知られた Northwind サンプルデータベースを使用します。
下は、"Products" テーブルからの抜粋です:
ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
---|---|---|---|---|---|
1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18 |
2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19 |
3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10 |
4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 21.35 |
5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 25 |
次の SQL 文は、product 名を選択すると共に、"Products" テーブルの price を最も近い整数に丸めます: