欢迎来到资源库(www.zyku.net)

MySQL

当前位置:首页 > 数据库 > MySQL > 格式化

mysql中格式化数字详解

时间:2017-07-12|栏目:MySQL|点击:|我要投稿

最近因为工作的需求,需要对mysql中数字进行格式化,但发现网上的资料较少,索性自己总结一下,方便自己也帮助有需要的朋友们,下面话不多说,来一起看看详细的介绍:

一、format函数:

格式化浮点数 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 给定位数,不足补充自定义字符

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

原文链接:http://www.cnblogs.com/duhuo/p/6250338.html

(资源库 www.zyku.net)

上一篇:MySQL中的数据类型binary和varbinary详解

栏    目:MySQL

下一篇:MySQL版本问题sql_mode=only_full_group_by的完美解决方案

本文标题:mysql中格式化数字详解

本文地址:https://www.zyku.net/mysql/1474.html

关于我们 | 版权申明 | 寻求合作 |

重要申明:本站所有的文章、图片、评论等内容,均由网友发表或上传并维护或收集自网络,仅供个人学习交流使用,版权归原作者所有。

如有侵犯您的版权,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:95148658 | 邮箱:mb8#qq.com(#换成@)

苏ICP备2020066115号-1

本网站由提供CDN加速/云存储服务