发布于 2017-10-24 21:28:05 | 204 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Mysql教程,程序狗速度看过来!

Mysql关系型数据库管理系统

MySQL是一个开放源码的小型关联式数据库管理系统,开发者为瑞典MySQL AB公司。MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。


格式化在我们日常开发中必不可少,最近在遇到了关于mysql格式化数字的需求,所以下面这篇文章主要给大家介绍了关于mysql中格式化数字的相关资料,分别介绍了format函数和rpad 和 lpad 给定位数,不足补充自定义字符,需要的朋友可以参考下。

最近因为工作的需求,需要对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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对PHPERZ的支持。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务