Heim  >  Artikel  >  Backend-Entwicklung  >  php格式化金额函数分享,php金额函数_PHP教程

php格式化金额函数分享,php金额函数_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:08:21830Durchsuche

php格式化金额函数分享,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         $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;
}

以上就是本文的全部内容,希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/951632.htmlTechArticlephp格式化金额函数分享,php金额函数 最近的项目在处理资金这一块的功能,对人民币金额的格式化输出是必不可少的功能。这个功能比较独...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn