This article mainly introduces the php formatting amount function sharing. If you need it, you can refer to the function of processing funds in the latest project, formatting the RMB amount is an essential function. This function is relatively independent and popular, so it is convenient to encapsulate it into a function.
The Code is as follows:
/**
* Format the 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 all the content of this article. I hope you will like it.