mysql中格式化数字详解
最近因为工作的需求,需要对mysql中数字进行格式化,但发现网上的资料较少,索性自己总结一下,方便自己也帮助有需要的朋友们,下面话不多说,来一起看看详细的介绍:
一、format函数:
格式化浮点数format(number,length);
介绍:FormatsthenumberXtoaformatlike'#,###,###.##',roundedtoDdecimalplaces,andreturnstheresultasastring.IfDis0,theresulthasnodecimalpointorfractionalpart.Dshouldbeaconstantvalue.
示例代码
mysql>SELECTFORMAT(12332.123456,4); ->'12,332.1235' mysql>SELECTFORMAT(12332.1,4); ->'12,332.1000' mysql>SELECTFORMAT(12332.2,0); ->'12,332'
二、rpad和lpad给定位数,不足补充自定义字符
RPAD:
Returnsthestringstr,right-paddedwiththestringpadstrtoalengthoflencharacters.If
strislongerthanlen,thereturnvalueisshortenedtolencharacters.
示例代码
mysql>SELECTRPAD('hi',5,'?'); ->'hi???' mysql>SELECTRPAD('hi',1,'?'); ->'h' mysql>SELETRPAD(12,5,0); ->12000
Thisfunctionismulti-bytesafe.
LPAD:
Returnsthestringstr,left-paddedwiththestringpadstrtoalengthoflencharacters.Ifstrislongerthanlen,thereturnvalueisshortenedtolencharacters.
示例代码
mysql>SELECTLPAD('hi',4,'??'); ->'??hi' mysql>SELECTLPAD('hi',1,'??'); ->'h' mysql>SELECTLPAD(12,5,0) ->'00012'
参考:http://www.cnblogs.com/fenglie/articles/4409208.html
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。