Home  >  Article  >  Backend Development  >  PHP formatted amount function sharing, PHP amount function_PHP tutorial

PHP formatted amount function sharing, PHP amount function_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:08:21833browse

Sharing of php formatted amount function, php amount function

The function of recent projects in handling funds is an indispensable function for the formatted output of RMB amounts. This function is relatively independent and popular, so it is convenient for everyone to encapsulate it into a function and send it up.

Copy code The code is as follows:

/**
* Format amount
*
* @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 = '';//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;
}

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/951632.htmlTechArticleSharing of php formatted amount function, php amount function’s recent project has the function of processing funds, for RMB amount Formatted output is an essential feature. This function is quite unique...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn