ROUND() 將數值四捨五入至指定小數位數,支援負數位數 (捨入至十位、百位)。
ROUND(column_name, decimals)
decimals > 0:小數點右側位數decimals = 0:四捨五入至整數decimals < 0:小數點左側 (十位、百位...)+----+------------+------+----------+
| Id | ProductName| Unit | UnitPrice|
+----+------------+------+----------+
| 1 | Jarlsberg |1000 g| 10.45 |
| 2 | Mascarpone |1000 g| 32.56 |
| 3 | Gorgonzola |1000 g| 15.67 |
+----+------------+------+----------+
SELECT ProductName, ROUND(UnitPrice, 0) AS RoundedPrice
FROM Products;
+------------+--------------+
| ProductName| RoundedPrice |
+------------+--------------+
| Jarlsberg | 10 |
| Mascarpone | 33 |
| Gorgonzola | 16 |
+------------+--------------+
SELECT ROUND(1234.56, -1); -- 1230 (捨入至十位)
SELECT ROUND(1234.56, -2); -- 1200 (捨入至百位)
ROUND 第三參數指定截斷而非四捨五入。DECIMAL 型態 + 應用層控制捨入模式。