mysql中格式化數(shù)字詳解
最近因為工作的需求,需要對mysql中數(shù)字進(jìn)行格式化,但發(fā)現(xiàn)網(wǎng)上的資料較少,索性自己總結(jié)一下,方便自己也幫助有需要的朋友們,下面話不多說,來一起看看詳細(xì)的介紹:
一、format函數(shù):
格式化浮點數(shù) format(number, length);
介紹:Formats the number X to a format like '#,###,###.##', rounded to D decimal places, and returns the result as a string. If D is 0, the result has no decimal point or fractional part. D should be a constant value.
示例代碼
mysql> SELECT FORMAT(12332.123456, 4); -> '12,332.1235' mysql> SELECT FORMAT(12332.1,4); -> '12,332.1000' mysql> SELECT FORMAT(12332.2,0); -> '12,332'
二、rpad 和 lpad 給定位數(shù),不足補充自定義字符
RPAD:
Returns the string str,right-padded with the string padstr to a length of len characters. If
str is longer than len, the return value is shortened to len characters.
示例代碼
mysql> SELECT RPAD('hi',5,'?'); -> 'hi???' mysql> SELECT RPAD('hi',1,'?'); -> 'h' mysql>SELET RPAD(12, 5 ,0); ->12000
This function is multi-byte safe.
LPAD:
Returns the string str, left-padded with the string padstr to a length of lencharacters. If str is longer than len, the return value is shortened to lencharacters.
示例代碼
mysql> SELECT LPAD('hi',4,'??'); -> '??hi' mysql> SELECT LPAD('hi',1,'??'); -> 'h' mysql>SELECT LPAD(12, 5 , 0) ->'00012'
參考:http://www.cnblogs.com/fenglie/articles/4409208.html
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
深入理解sqlserver中的字符編碼、排序規(guī)則、nvarchar和varchar
本文主要介紹了深入理解sqlserver中的字符編碼、排序規(guī)則、nvarchar和varchar,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09MySQLBackup備份數(shù)據(jù)庫的操作過程
這篇文章主要介紹了MySQLBackup備份數(shù)據(jù)庫的操作過程,包括安裝部署和配置備份管理員的相關(guān)知識,本文給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03使用navicat 8實現(xiàn)創(chuàng)建數(shù)據(jù)庫和導(dǎo)入數(shù)據(jù) 管理用戶與權(quán)限[圖文方法]
使用navicat8實現(xiàn)創(chuàng)建數(shù)據(jù)庫和導(dǎo)入數(shù)據(jù)的方法,需要的朋友可以參考下。2011-04-04