PHP converts a number to an uppercase value. _ php instance

Source: Internet
Author: User
Tags rtrim
This article mainly introduces the function sharing of converting numbers into RMB in capital in PHP. The final effect is based on a Baidu APP to meet the needs of most cases, if you need a friend, you can refer to the financial person who has encountered such a problem, for example, 13,126.8 yuan, in the invoice or other accounting business, it is generally necessary to use uppercase letters for spelling as "one million yuan, one yuan ".

This is not the case. I also attached a php function that I used to be a student/car network for your reference.

The code is as follows:


/**
* Lowercase to uppercase
*
* @ Param string $ number value
* @ Param string $ int_unit currency unit. the default value is "RMB". some requirements may be "RMB"
* @ Param bool $ is_round: whether to round the decimal number
* @ Param bool $ is_extra_zero: whether to end the integer with 0, and add 0 to the decimal number, such as 1960.30
* @ Return string
*/
Function RMB _format ($ money = 0, $ int_unit = 'meta', $ is_round = true, $ is_extra_zero = false ){
// Split the number into two segments
$ Parts = explode ('.', $ money, 2 );
$ Int = isset ($ parts [0])? Strval ($ parts [0]): '0 ';
$ Dec = isset ($ parts [1])? Strval ($ parts [1]): '';

// If there are more than two digits after the decimal point, it will be intercepted directly without rounding. Otherwise, it will be processed.
$ Dec_len = strlen ($ dec );
If (isset ($ parts [1]) & $ dec_len> 2 ){
$ Dec = $ is_round? Substr (strrchr (strval (round (floatval ("0. ". $ dec), 2 )),'. '), 1): substr ($ parts [1], 0, 2 );
}

// When number is 0.001, the amount after the decimal point is 0 yuan
If (empty ($ int) & empty ($ dec )){
Return '0 ';
}

// Define
$ Chs = array ('0', 'yi', 'er', 'san', 'siz', 'wu', 'L', 'region ', 'authorization', 'authorization ');
$ Uni = array ('', 'pick', 'handler', 'handler ');
$ Dec_uni = array ('angular ', 'shard ');
$ Exp = array ('', 'wan ');
$ Res = '';

// Locate the integer from the right to the left
For ($ I = strlen ($ int)-1, $ k = 0; $ I >=0; $ k ++ ){
$ Str = '';
// According to the Chinese reading and writing habits, each 4 words is converted into a segment, and I has been decreasing
For ($ j = 0; $ j <4 & $ I> = 0; $ j ++, $ I --){
$ U = $ int {$ I}> 0? $ Uni [$ j]: ''; // add a unit after a number other than 0
$ Str = $ chs [$ int {$ I}]. $ u. $ str;
}
$ Str = rtrim ($ str, '0'); // remove the end 0
$ Str = preg_replace ("/0 +/", "zero", $ str); // replace multiple consecutive 0
If (! Isset ($ exp [$ k]) {
$ Exp [$ k] = $ exp [$ k-2]. '100 '; // Build unit
}
$ U2 = $ str! = ''? $ Exp [$ k]: '';
$ Res = $ str. $ u2. $ res;
}

// If the decimal part is 00 after processing
$ Dec = rtrim ($ dec, '0 ');
Var_dump ($ dec );
// Find the fractional part from left to right
If (! Empty ($ dec )){
$ Res. = $ int_unit;

// Whether to append 0 to the number ending with 0 in the integer part. some systems have this requirement.
If ($ is_extra_zero ){
If (substr ($ int,-1) = '0 '){
$ Res. = '0 ';
}
}

For ($ I = 0, $ cnt = strlen ($ dec); $ I <$ cnt; $ I ++ ){
$ U = $ dec {$ I}> 0? $ Dec_uni [$ I]: ''; // add a unit after a number other than 0
$ Res. = $ chs [$ dec {$ I}]. $ u;
If ($ cnt = 1)
$ Res. = 'integral ';
}

$ Res = rtrim ($ res, '0'); // remove the end 0
$ Res = preg_replace ("/0 +/", "zero", $ res); // replace multiple consecutive 0
} Else {
$ Res. = $ int_unit. 'integral ';
}
Return $ res;
}

Easy to use

The code is as follows:


USD yuan = 13598.3;
$ Ret = RMB _format ($ yuan );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.