php格式化金额函数分享
2016-06-10 13:08
537 查看
/**
* 格式化金额
*
* @param int $money
* @param int $len
* @param string $sign
* @return string
*/
function format_money($money, $len=2, $sign='¥'){
$negative = $money > 0 ? '' : '-';
$int_money = intval(abs($money));
$len = intval(abs($len));
$decimal = '';//小数
if ($len > 0) {
$decimal = '.'.substr(sprintf('%01.'.$len.'f', $money),-$len);
}
$tmp_money = strrev($int_money);
$strlen = strlen($tmp_money);
for ($i = 3; $i < $strlen; $i += 3) {
$format_money .= substr($tmp_money,0,3).',';
$tmp_money = substr($tmp_money,3);
}
$format_money .= $tmp_money;
$format_money = strrev($format_money);
return $sign.$negative.$format_money.$decimal;
}
* 格式化金额
*
* @param int $money
* @param int $len
* @param string $sign
* @return string
*/
function format_money($money, $len=2, $sign='¥'){
$negative = $money > 0 ? '' : '-';
$int_money = intval(abs($money));
$len = intval(abs($len));
$decimal = '';//小数
if ($len > 0) {
$decimal = '.'.substr(sprintf('%01.'.$len.'f', $money),-$len);
}
$tmp_money = strrev($int_money);
$strlen = strlen($tmp_money);
for ($i = 3; $i < $strlen; $i += 3) {
$format_money .= substr($tmp_money,0,3).',';
$tmp_money = substr($tmp_money,3);
}
$format_money .= $tmp_money;
$format_money = strrev($format_money);
return $sign.$negative.$format_money.$decimal;
}
相关文章推荐
- PHP数组排序和按数量分割
- PHP获取当前日期和时间及格式化方法参数
- 解决FILEZILLA CLIENT工具无法获取FTP目录的错误
- Filezilla Server支持FTP的Passive被动模式
- 跟我学习php字符串常用函数-下篇
- php单例模式的简单实现方法
- PHP分页代码
- php数组方法
- OK6410之UBOOT增加TFTP传送烧写IMAEG功能
- PHP写入数据库中文乱码问题
- php获取全选checkbox多个值
- 开启FTP服务器(配置)
- 第24课:Spark Streaming的Transformation、Action、Input和Output
- php Compile on Mac
- FTP资料
- PHP Cookie处理函数
- php英文单词统计器
- php单例模式的简单实现方法
- 浅谈PHP Cookie处理函数
- YII的CMenu使用中url和active的问题