在 MySQL 中選擇具有給定精度的 FLOAT


可以使用 ROUND() 函式。

語法如下

SELECT ROUND(yourColumnName,yourPrecisionIntegerValue) from yourTableName;

要理解這個概念,我們建立一個表格。建立表格的查詢如下

mysql> create table givenPrecisionDemo
   -> (
   -> Amount float
   -> );
Query OK, 0 rows affected (0.61 sec)

使用插入命令在表中插入一些記錄。查詢如下 −

mysql> insert into givenPrecisionDemo(Amount) values(45.678);
Query OK, 1 row affected (0.12 sec)
mysql> insert into givenPrecisionDemo(Amount) values(123.456);
Query OK, 1 row affected (0.15 sec)
mysql> insert into givenPrecisionDemo(Amount) values(245.890);
Query OK, 1 row affected (0.20 sec)
mysql> insert into givenPrecisionDemo(Amount) values(980.678);
Query OK, 1 row affected (0.19 sec)

使用 select 語句顯示錶中的所有記錄。查詢如下 −

mysql> select *from givenPrecisionDemo;

以下是輸出

+---------+
| Amount  |
+---------+
| 45.678  |
| 123.456 |
| 245.89  |
| 980.678 |
+---------+
4 rows in set (0.00 sec)

以下是選擇具有給定精度的 FLOAT 的查詢

mysql> SELECT ROUND(Amount,2) from givenPrecisionDemo;

以下是輸出

+-----------------+
| ROUND(Amount,2) |
+-----------------+
| 45.68           |
| 123.46          |
| 245.89          |
| 980.68          |
+-----------------+
4 rows in set (0.00 sec)

更新於: 2019 年 7 月 30 日

1K+ 閱讀量

開啟 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.