Php converts money numbers into upper-case functions

Source: Internet
Author: User
Tags chop error handling numeric numeric value rtrim strlen

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 point
If ($ 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-> right
If ($ 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-> right
If ($ 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 [4]) // filter duplicate "tens of thousands"
 {
$ Final [$ y ++] = $ chinses [$ x];
$ Inwan = 1;
 }
If ($ inyi = 0 & $ chinses [$ x] = $ e [8]) // filter duplicate ""
 {
$ Final [$ y ++] = $ chinses [$ x];
$ Inyi = 1;
$ Inwan = 0;
 }
If ($ chinses [$ x]! = $ E [4] & $ chinses [$ x]! = $ E [8]) // 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 found
Return $ 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

<? Php
/**
* 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,
* Some systems require the output of "one hacker, and one hacker".
* @ Return string
* @ Site www.111cn.net */
Function num2rmb ($ number = 0, $ int_unit = 'meta', $ is_round = TRUE, $ is_extra_zero = FALSE)
    {
// Split the number into two segments
$ Parts = <A class = infotextkey href = "http: // www." target = _ blank> explode </A> ('.', $ number, 2 );
$ Int = <A class = infotextkey href = "http://www.111cn.net/" target = _ blank> isset </A> ($ 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;
            }
// Echo $ str. "|". ($ k-2). "<br> ";
$ 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 ');

// 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;
            }
$ 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 "<pre> ";
$ 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 out

Return $ c;
} // End of function

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.