在 MySQL 中使用数字对记录进行四舍五入
mysqlmysqli database更新于 2023/11/5 10:48:00
要对数字进行四舍五入,请使用 MySQL ROUND()。让我们首先创建一个表 −
mysql> create table DemoTable -> ( -> Amount DECIMAL(10,4) -> ); Query OK, 0 rows affected (1.18 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values(100.578); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(1000.458); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(980.89); Query OK, 1 row affected (0.13 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable;
这将产生以下输出 −
+-----------+ | Amount | +-----------+ | 100.5780 | | 1000.4580 | | 980.8900 | +-----------+ 3 rows in set (0.00 sec)
这是对数字进行四舍五入的查询 −
mysql> update DemoTable set Amount=round(Amount); Query OK, 3 rows affected (0.12 sec) Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录 −
mysql> select * from DemoTable;
这将产生以下输出 −
+-----------+ | Amount | +-----------+ | 101.0000 | | 1000.0000 | | 981.0000 | +-----------+ 3 rows in set (0.00 sec)