A function for converting Arabic numerals to Chinese numerals

Source: Internet
Author: User
A function used to convert Arabic numerals to Chinese numerals. after reading a function for converting Arabic numerals to Chinese numerals, I recently wrote a function called "Arabic numerals to Chinese numerals ". Only one similar one is found in the search for the essence area. I feel that my algorithm is good, so I posted it and shared it. if I want to use it for the amount conversion, do I have to modify the processing of the decimal part? Phpfu "> <LINKhref =" http://www.php100.com

Recently, as a result of this need, I wrote a "Arabic numerals to Chinese numerals" function ". Only one similar one is found in the search for the essence area.
I felt that my algorithm was good, so I posted it and shared it. if you want to convert the amount, you need to modify the processing of the decimal part.
Function ch_num ($ num, $ mode = true ){
$ Char = array ("zero", "one", "two", "three", "si", "Wu", "Lu", "Jun ", "Success", "fail ");
$ Dw = array ("", "pick", "audio", "audio", "", "audio", "audio", "MB ");
$ Dec = "point ";
$ Retval = "";

If ($ mode)
Preg_match_all ("/^ 0 * (\ d *)\.? (\ D *)/", $ num, $ ar );
Else
Preg_match_all ("/(\ d *)\.? (\ D *)/", $ num, $ ar );

If ($ ar [2] [0]! = "")
$ Retval = $ dec. ch_num ($ ar [2] [0], false); // if there is a decimal number, first recursively process the decimal number
If ($ ar [1] [0]! = ""){
$ Str = strrev ($ ar [1] [0]);
For ($ I = 0; $ I $ Out [$ I] = $ char [$ str [$ I];
If ($ mode ){
$ Out [$ I]. = $ str [$ I]! = "0 "? $ Dw [$ I % 4]: "";
If ($ str [$ I] + $ str [$ i-1] = 0)
$ Out [$ I] = "";
If ($ I % 4 = 0)
$ Out [$ I]. = $ dw [4 + floor ($ I/4)];
}
}
$ Retval = join ("", array_reverse ($ out). $ retval;
}
Return $ retval;
}

// Echo ch_num ("12345006789001.123 ");
// Echo ch_num ("880079.1234 ");
Echo ch_num ("300045.0123 ");

?>

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.