msyql的FORMAT()函数设置小数位数,将小数位数保留到小数点后第n位,最后一位四舍五入
生活随笔
收集整理的這篇文章主要介紹了
msyql的FORMAT()函数设置小数位数,将小数位数保留到小数点后第n位,最后一位四舍五入
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
FORMAT()函數(shù)
??在mysql的查詢中,如果我們輸入的是小數(shù)類型,而保留幾位小數(shù)因情況而變,我們可以用FORMAT()函數(shù)來設(shè)置想保留的幾位小數(shù)。即將數(shù)據(jù)內(nèi)容格式化,將數(shù)據(jù)格式化為整數(shù)或者帶幾位小數(shù)的浮點(diǎn)數(shù)(四舍五入)。
語法
SELECT FORMAT(a, b) FROM table;其中,a為你要操作的字段,b為保留幾位小數(shù)
舉例理解
sql語句
創(chuàng)建表:
create table formathanshu( price DOUBLE not null primary key, name varchar(20) not null )charset=utf8;加入數(shù)據(jù):
insert into formathanshu values(500.999,'The Journey to the West'); insert into formathanshu values(4.6777,'The Dream of Red Mansion'); insert into formathanshu values(99.999999,'The Water margin'); insert into formathanshu values(33.3,'Romance of the Three Kingdoms'); insert into formathanshu values(77.988,'Border town');運(yùn)行結(jié)果
要求
將price字段的所有數(shù)字保留兩位小數(shù)
sql語句
運(yùn)行結(jié)果
如果你想保留整數(shù),那么將后面的數(shù)字設(shè)為0就OK啦!
https://blog.csdn.net/hanhanwanghaha寶藏女孩 歡迎您的關(guān)注!
歡迎關(guān)注微信公眾號:寶藏女孩的成長日記
如有轉(zhuǎn)載,請注明出處(如不注明,盜者必究)
總結(jié)
以上是生活随笔為你收集整理的msyql的FORMAT()函数设置小数位数,将小数位数保留到小数点后第n位,最后一位四舍五入的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HTTP协议理解——计算机网络
- 下一篇: Linux用户、权限及改变文件所有者及文