MySQL 格式化函数


格式化函数

在这里插入图片描述

我们经常会要求按照某种格式输出数据,以方便我们阅读、快速掌握数据信息。前面学习的日期时间函数其实就是一种格式化函数,通过函数把日期转化为我们需要的格式。

接下来给大家介绍的格式化函数是 format() 函数。使用该函数可得到 ##,###.#### 这种格式的输出。其基本语法如下:

format(value,n)

注意:其中 value 是被格式化的数据,n 表示保留的小数位数。

1. 对整数进行格式化,带 2 位小数

MariaDB [(none)]> select format(1000,2);
+----------------+
| format(1000,2) |
+----------------+
| 1,000.00       |
+----------------+
1 row in set (0.000 sec)

2. 对浮点数进行格式化,带 2 位小数

MariaDB [(none)]> select format(1000.145,2);
+--------------------+
| format(1000.145,2) |
+--------------------+
| 1,000.15           |
+--------------------+
1 row in set (0.000 sec)

结果解析:可以看到该函数具有四舍五入的功能。

猜你喜欢

转载自blog.csdn.net/m0_62617719/article/details/130842550
今日推荐