Php converts money numbers into upper-case functions

Source: Internet
Author: User

Example 1

 The code is as follows: Copy code Function change_num (\$ num) {\$ D = array ('0', 'yi', 'er', 'san', 'siz', 'wu', 'L', 'region ', 'authorization', 'authorization ');\$ E = array ('meta', 'pick', 'Taobao', 'Taobao', 'wan ', 'wan Wan ', 'Billions ', 'hundreds of billions', and 'trillions ');\$ P = array ('quantity', 'angular ');\$ Zheng = 'integral '; // Append the word "integral".\$ Final = array (); // result\$ Inwan = 0; // whether there are tens of thousands\$ Inyi = 0; // whether there are hundreds of millions\$ Len_pointdigit = 0; // length after decimal point\$ Y = 0;If (\$ c = strpos (\$ num, '.') // you can specify the number of digits before the decimal point. { \$ Len_pointdigit = strlen (\$ num)-strpos (\$ num, '.')-1; // determine the number of digits after the decimal pointIf (\$ c> 13) // simple error handling {Echo "the amount is too large to exceed 1 trillion .";Die (); }Elseif (\$ len_pointdigit> 2) // The number of digits after the decimal point in \$ len_pointdigit {Echo "only two decimal places are supported .";Die (); } }Else // no decimal point {\$ C = strlen (\$ num );\$ Zheng = 'integral '; }For (\$ I = 0; \$ I <\$ c; \$ I ++) // process the integer part {\$ Bit_num = substr (\$ num, \$ I, 1); // read left-> rightIf (\$ bit_num! = 0 | substr (\$ num, \$ I + 1, 1 )! = 0) // if the current value is zero and the next digit is zero or zero, it is not displayed.@ \$ Low2chinses = \$ low2chinses. \$ d [\$ bit_num];If (\$ bit_num | \$ I = \$ C-1)@ \$ Low2chinses = \$ low2chinses. \$ e [\$ c-\$ i-1]; }For (\$ j = \$ len_pointdigit; \$ j> = 1; \$ j --) // process the fractional part {\$ Point_num = substr (\$ num, strlen (\$ num)-\$ j, 1); // read the left-> rightIf (\$ point_num! = 0)@ \$ Low2chinses = \$ low2chinses. \$ d [\$ point_num]. \$ p [\$ j-1];// If (substr (\$ num, strlen (\$ num)-2, 1) = 0 & substr (\$ num, strlen (\$ num)-1, 1) = 0) // Both digits after the decimal point are 0 }\$ Chinses = str_split (\$ low2chinses, 2); // convert a string to an array// Print_r (\$ chinses );For (\$ x = sizeof (\$ chinses)-1; \$ x> = 0; \$ x --) // filter invalid information {If (\$ inwan = 0 & \$ chinses [\$ x] = \$ e ) // filter duplicate "tens of thousands" {\$ Final [\$ y ++] = \$ chinses [\$ x];\$ Inwan = 1; }If (\$ inyi = 0 & \$ chinses [\$ x] = \$ e ) // filter duplicate "" {\$ Final [\$ y ++] = \$ chinses [\$ x];\$ Inyi = 1;\$ Inwan = 0; }If (\$ chinses [\$ x]! = \$ E  & \$ chinses [\$ x]! = \$ E ) // sort and assign the final value to the \$ final array\$ Final [\$ y ++] = \$ chinses [\$ x]; }\$ Newstring = (array_reverse (\$ final); // \$ final is the reciprocal group, and \$ newstring is an array that can be used normally.\$ Nstring = join (\$ newstring); // The array is converted into a string.If (substr (\$ num,-2, 1) = 0 & substr (\$ num,-1) <> 0) // Determine if the original amount is 0? Not 0? { \$ Nstring = substr (\$ nstring, 0, (strlen (\$ nstring)-4 )). "Zero ". substr (\$ nstring,-); // add a zero character }\$ Fen = "points ";\$ Fj = substr_count (\$ nstring, \$ fen); // if no score is foundReturn \$ nstring = (\$ fj = 0 )? \$ Nstring. \$ zheng: \$ nstring; // add the "whole" to the end. }

Example 2

1. Supports astronomical numbers, and the whole number can be infinitely long in theory;
2. Decimal places are supported. For currencies, it is generally accurate to the last two digits of the decimal places. You can set whether to round the decimal places;
3. Custom currency units are supported. Some systems require "yuan" in upper case, and some require "yuan" in custom mode;
4. allows you to add "zero" to the end of a number that ends with 0 and contains decimal numbers. For example, some systems require that numbers like 1960.30 be converted to uppercase and then "", however, some systems require "one party, two parties.

 The code is as follows: Copy code explode ('.', \$ number, 2 );\$ Int = isset (\$ parts )? Strval (\$ parts ): '0 ';\$ Dec = isset (\$ parts )? Strval (\$ parts ): '';// 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 ) & \$ dec_len> 2)        {\$ Dec = \$ is_round? Substr (strrchr (strval (round (floatval ("0.". \$ dec), 2), '.'), 1): Substr (\$ parts , 0, 2 );        }// When number is 0.001, the amount after the decimal point is 0 yuanIf (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 leftFor (\$ 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 decreasingFor (\$ 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;            }// Echo \$ str. "|". (\$ k-2). "
";\$ Str = rtrim (\$ str, '0'); // remove the end 0\$ Str = preg_replace ("/0 +/", "zero", \$ str); // replace multiple consecutive 0If (! 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 ');// Find the fractional part from left to rightIf (! 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;            }\$ 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;    }Echo "
";\$ Number = "1000000000000000012345678900.501 ";Echo \$ number. ":". num2rmb (\$ number );Echo "n ";\$ Number = "1960.30 ";Echo \$ number. ":". num2rmb (\$ number );Echo "n ";\$ Number = "1960.30 ";Echo \$ number. ":". num2rmb (\$ number, "circle", true, true );Echo "n ";\$ Number = "123456789.005 ";Echo \$ number. ":". num2rmb (\$ number );Echo "n ";\$ Number = "123456789.005 ";Echo \$ number. ":". num2rmb (\$ number, "RMB", false );Echo "n ";\$ Number = "10000000000000000060009.101 ";Echo \$ number. ":". num2rmb (\$ number );Echo "n ";\$ Number = "1680.32 ";Echo \$ number. ":". num2rmb (\$ number );?>

Example 3

The above person prefers the following

 The code is as follows: Copy code // Converts the numeric amount to an uppercase numeric value.Function num2rmb (\$ num ){\$ C1 = "";\$ C2 = "yuanqibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaibaiyi ";\$ Num = round (\$ num, 2 );\$ Num = \$ num * 100;\$ NewNum = ceil (\$ num );If (strlen (\$ NewNum)> 10 ){Return "The amount is too large "; }\$ I = 0;\$ C = "";While (1 ){If (\$ I = 0 ){\$ N = substr (\$ num, strlen (\$ num)-1, 1 );} Else {\$ N = \$ num % 10; }\$ P1 = substr (\$ c1, 2 * \$ n, 2 );\$ P2 = substr (\$ c2, 2 * \$ I, 2 );If (\$ n! = '0' | (\$ n = '0' & (\$ p2 = '000000' | \$ p2 = '000000' | \$ p2 =' yuan '))) {\$ C = \$ p1. \$ p2. \$ c;} Else {\$ C = \$ p1. \$ c; }\$ I = \$ I + 1;\$ Num = \$ num/10;\$ Num = (int) \$ num;If (\$ num = 0 ){Break; }} // End of while | here, we got a chinese string with some useless character// We chop out the useless characters to form the correct output\$ J = 0;\$ Slen = strlen (\$ c );While (\$ j <\$ slen ){\$ M = substr (\$ c, \$ j, 4 );If (\$ m = 'zero meta' | \$ m = '000000' | \$ m = '000000' | \$ m = 'zero-zero '){\$ Left = substr (\$ c, 0, \$ j );\$ Right = substr (\$ c, \$ j + 2 );\$ C = \$ left. \$ right;\$ J = \$ J-2;\$ Slen = \$ slen-2; }\$ J = \$ j + 2; }If (substr (\$ c, strlen (\$ c)-2, 2) = '0 '){\$ C = substr (\$ c, 0, strlen (\$ c)-2 );} // If there is a '0' on the end, chop it outReturn \$ c;} // End of function
Related Keywords:
Related Article E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth \$300-1200 USD