MySQL 如何将数字格式化为2位小数

MySQL 如何将数字格式化为2位小数

您可以使用MySQL中的TRUNCATE()函数将数字格式化为2位小数。 语法如下 –

SELECT TRUNCATE(yourColumnName,2) as anyVariableName from yourTableName;
Mysql

为了了解上面的语法,让我们首先创建一个表。 创建表的查询如下 –

mysql> create table FormatNumberTwoDecimalPlace
-> (
-> Number float
-> );
Query OK, 0 rows affected (0.59 sec)
Mysql

使用插入命令在表中插入一些记录。 查询如下 –

mysql> insert into FormatNumberTwoDecimalPlace values(123.456);
Query OK, 1 row affected (0.13 sec)

mysql> insert into FormatNumberTwoDecimalPlace values(1.6789);
Query OK, 1 row affected (0.19 sec)

mysql> insert into FormatNumberTwoDecimalPlace values(12.2);
Query OK, 1 row affected (0.14 sec)

mysql> insert into FormatNumberTwoDecimalPlace values(12356.23145);
Query OK, 1 row affected (0.40 sec)

mysql> insert into FormatNumberTwoDecimalPlace values(12356);
Query OK, 1 row affected (0.14 sec)

mysql> insert into FormatNumberTwoDecimalPlace values(.5678);
Query OK, 1 row affected (0.28 sec)
Mysql

现在让我们使用select命令显示来自表的所有记录。 查询如下 –

mysql> select *from FormatNumberTwoDecimalPlace;
Mysql

阅读更多:MySQL 教程

输出

+---------+
| Number  |
+---------+
| 123.456 |
|  1.6789 |
|   12.2  |
| 12356.2 |
|  12356  |
|  0.5678 |
+---------+
6 rows in set (0.04 sec)
Mysql

以下是格式化数字为两位小数的查询 –

mysql> select truncate(Number,2) as TwoValueAfterDecimal from
FormatNumberTwoDecimalPlace;
Mysql

输出

+----------------------+
| TwoValueAfterDecimal |
+----------------------+
|               123.45 |
|                 1.67 |
|                12.19 |
|             12356.23 |
|             12356.00 |
|                 0.56 |
+----------------------+
6 rows in set (0.00 sec)
Mysql

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册