码迷,mamicode.com
首页 > Web开发 > 详细

php 格式化金额

时间:2015-02-01 18:58:15      阅读:397      评论:0      收藏:0      [点我收藏+]

标签:

 1 /**
 2  * 格式化金额
 3  *
 4  * @param int $money
 5  * @param int $len
 6  * @param string $sign
 7  * @return string
 8  */
 9 function format_money($money, $len=2, $sign=‘¥‘){
10     $negative = $money > 0 ? ‘‘ : ‘-‘;
11     $int_money = intval(abs($money));
12     $len = intval(abs($len));
13     $decimal = ‘‘;//小数
14     if ($len > 0) {
15         $decimal = ‘.‘.substr(sprintf(‘%01.‘.$len.‘f‘, $money),-$len);
16     }
17     $tmp_money = strrev($int_money);
18     $strlen = strlen($tmp_money);
19     for ($i = 3; $i < $strlen; $i += 3) {
20         $format_money .= substr($tmp_money,0,3).‘,‘;
21         $tmp_money = substr($tmp_money,3);
22     }
23     $format_money .= $tmp_money;
24     $format_money = strrev($format_money);
25     return $sign.$negative.$format_money.$decimal;
26 }

 

php 格式化金额

标签:

原文地址:http://www.cnblogs.com/jackoogle/p/4265825.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!