Php converts the number 1-1 million into Chinese characters, for example, 150 to one hundred and fifty, 1-1150

Source: Internet
Author: User

Php converts the number 1-1 million into Chinese characters, for example, 150 to one hundred and fifty, 1-1150

Directly mount the instance

It has been written to hundreds of billions.

/**

* @ Author ja Song
* Replace 1-1 million numbers with Chinese characters, for example, 123-> one hundred and twenty-three
* @ Param [num] $ num [number]
* @ Return [string] [string]
*/
Function numToWord ($ num)
{
$ ChiNum = array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9 ');
$ ChiUni = array ('', '10', '100', '100', '100', '100', '100', '100 ', 'kil ');

$ ChiStr = '';


$ Num_str = (string) $ num;

$ Count = strlen ($ num_str );
$ Last_flag = true; // whether the previous one is 0
$ Zero_flag = true; // whether the first flag is used
$ Temp_num = null; // temporary number

$ ChiStr = ''; // splicing result
If ($ count = 2) {// double digits
$ Temp_num = $ num_str [0];
$ ChiStr = $ temp_num = 1? $ ChiUni [1]: $ chiNum [$ temp_num]. $ chiUni [1];
$ Temp_num = $ num_str [1];
$ ChiStr. = $ temp_num = 0? '': $ ChiNum [$ temp_num];
} Else if ($ count> 2 ){
$ Index = 0;
For ($ I = $ count-1; $ I> = 0; $ I --){
$ Temp_num = $ num_str [$ I];
If ($ temp_num = 0 ){
If (! $ Zero_flag &&! $ Last_flag ){
$ ChiStr = $ chiNum [$ temp_num]. $ chiStr;
$ Last_flag = true;
}
} Else {
$ ChiStr = $ chiNum [$ temp_num]. $ chiUni [$ index % 9]. $ chiStr;

$ Zero_flag = false;
$ Last_flag = false;
}
$ Index ++;
}
} Else {
$ ChiStr = $ chiNum [$ num_str [0];
}
Return $ chiStr;
}


$ Num = 150;
Echo numToWord ($ num );

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.