php格式化金额函数分享
/**
?* 格式化金額
?*
?* @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;
}
轉載于:https://www.cnblogs.com/lucktian/p/5573565.html
總結
以上是生活随笔為你收集整理的php格式化金额函数分享的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Printk与sched_clock_i
- 下一篇: 动态规划算法php,php算法学习之动态