如何在MySQL中将数字格式化为2个小数位?
您可以使用TRUNCATE()MySQL中的函数将数字格式化为2个小数位。语法如下-
SELECT TRUNCALTE(yourColumnName,2) as anyVariableName from yourTableName;
为了理解上述语法,让我们首先创建一个表。创建表的查询如下-
mysql> create table FormatNumberTwoDecimalPlace -> ( -> Number float -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into FormatNumberTwoDecimalPlace values(123.456); mysql> insert into FormatNumberTwoDecimalPlace values(1.6789); mysql> insert into FormatNumberTwoDecimalPlace values(12.2); mysql> insert into FormatNumberTwoDecimalPlace values(12356.23145); mysql> insert into FormatNumberTwoDecimalPlace values(12356); mysql> insert into FormatNumberTwoDecimalPlace values(.5678);
现在,让我们使用select命令显示表中的所有记录。查询如下-
mysql> select *from FormatNumberTwoDecimalPlace;
输出结果
+---------+ | Number | +---------+ | 123.456 | | 1.6789 | | 12.2 | | 12356.2 | | 12356 | | 0.5678 | +---------+ 6 rows in set (0.04 sec)
这是将数字格式化为两位小数的查询-
mysql> select truncate(Number,2) as TwoValueAfterDecimal from FormatNumberTwoDecimalPlace;
输出结果
+----------------------+ | TwoValueAfterDecimal | +----------------------+ | 123.45 | | 1.67 | | 12.19 | | 12356.23 | | 12356.00 | | 0.56 | +----------------------+ 6 rows in set (0.00 sec)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短