您可以使用MySQL的TRUNCATE()函數將數字格式化為2位小數。語法如下:
SELECT TRUNCATE(yourColumnName,2) as anyVariableName from yourTableName;
登錄后復制
為了理解上面的語法,讓我們首先創建一個表。創建表的查詢如下 –
mysql> create table FormatNumberTwoDecimalPlace -> ( -> Number float -> ); Query OK, 0 rows affected (0.59 sec)
登錄后復制
使用插入命令在表中插入一些記錄。查詢如下所示 ?
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)
登錄后復制
讓我們現在使用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)
登錄后復制
以上就是MySQL中如何將數字格式化為小數點后兩位?的詳細內容,更多請關注www.92cms.cn其它相關文章!