mysql 格式化数字入门实例

感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧!

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

mysql>SELECT LPAD(12,0)
  ->'00012'

相关文章

在正式开始之前,我们先来看下 MySQL 服务器的配置和版本号信...
> [合辑地址:MySQL全面瓦解](https://www.cnblogs.c...
物理服务机的CPU、内存、存储设备、连接数等资源有限,某个时...
1 回顾 上一节我们详细讲解了如何对数据库进行分区操作,包括...
navicat查看某个表的所有字段的详细信息 navicat设计表只能一...
文章浏览阅读4.3k次。转载请把头部出处链接和尾部二维码一起...