数据库
首页 > 数据库> > mysql – 如何将列的值设为2个小数点?

mysql – 如何将列的值设为2个小数点?

作者:互联网

CODE SYNTAX

从列中可以看出,我希望输出为2位小数,如EndOfYearForecast所示.有什么方法吗?enter image description here

解决方法:

您可以使用ROUND()功能

SELECT
CONCAT("£",ROUND(SUM(cost),2)) AS EndOfYearForecast,
CONCAT("£",ROUND(SUM(cost/12),2)) AS MonthlyAverage,
CONCAT("£",ROUND(SUM(cost/53),2)) AS WeeklyAverage
FROM yearlyAnalysis;

如果您想在显示屏中使用逗号,也可以使用FORMAT()功能

SELECT
CONCAT("£",FORMAT(SUM(cost),2)) AS EndOfYearForecast,
CONCAT("£",FORMAT(SUM(cost/12),2)) AS MonthlyAverage,
CONCAT("£",FORMAT(SUM(cost/53),2)) AS WeeklyAverage
FROM yearlyAnalysis;

mysql> SET @NUM=1234.56789; SELECT ROUND(@NUM,2) RoundNum,FORMAT(@NUM,2) FormatNum;
Query OK, 0 rows affected (0.00 sec)

+----------+-----------+
| RoundNum | FormatNum |
+----------+-----------+
|  1234.57 | 1,234.57  |
+----------+-----------+
1 row in set (0.00 sec)

mysql>

标签:query-performance,mysql
来源: https://codeday.me/bug/20190806/1602868.html