如何对 MySQL 中的浮点值列进行四舍五入,并在新列中显示结果?
让我们先创建一个表 -
mysql> create table DemoTable ( Value float ); Query OK, 0 rows affected (0.95 sec)
使用插入命令在表中插入一些记录 -
mysql> insert into DemoTable values(12.4567); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values(124.7884); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(45.64643); Query OK, 1 row affected (0.46 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+---------+ | Value | +---------+ | 12.4567 | | 124.788 | | 45.6464 | +---------+ 3 rows in set (0.00 sec)
以下是用于四舍五入浮点记录并在新列中显示结果的查询 -
mysql> select Value,ROUND(Value,2) AS RoundValue from DemoTable;
这将产生以下输出 -
+---------+------------+ | Value | RoundValue | +---------+------------+ | 12.4567 | 12.46 | | 124.788 | 124.79 | | 45.6464 | 45.65 | +---------+------------+ 3 rows in set (0.00 sec)
广告