Convert an integer to a Chinese character, for example, [101]-> [one hundred and one]

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn the code for converting Chinese characters to numbers released yesterday. At the request of netizens, write another inversion algorithm, no problem was found after a simple test. Now I will share the Code. If you are interested, I can test the Code. If you have better algorithms, please share them with me, it's better to use original code!
Test2 ();

/**
* Method test
*/
Function test2 (){
Echo '2014. 22 => '. IntToCn (333030 ).' '; // 333,030
Echo '2014 => '. IntToCn (1111 ).' '; // One thousand one hundred and eleven
Echo '2014 => '. IntToCn (101010101 ).' '; // 0.1 billion 1,010,101
}

/**
* Convert numbers to Chinese Characters
* @ Param Int $ num the number to be parsed
* @ Param String $ string Initial Value
* @ Return String
* @ Author Anyon Zou
* @ Date
*/
Function IntToCn ($ num, $ string = array ()){
If (! Is_numeric ($ num )){
Return $ num;
}
$ Splits = array ('000000' => '000000', '000000' => '000000 ');
$ Chars = array ('000000' => '000000', '000000' => 'kil', '000000' => '100 ', '10' => '10', '1' => '', '0' => '0 ');
$ Ints = array ('0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', '10 ');

/*
* Split integers and Decimals
*/
$ Nums = explode ('.', "{$ num }");
$ Num = $ nums [0];
// Process Decimals
$ Dou = array ();
If (! Empty ($ nums [1]) {
Foreach (str_split ("{$ nums [1]}") as $ n ){
If (is_numeric ($ n )){
$ Dou [] = $ ints [intval ($ n)];
}
}
}
Unset ($ nums );

Foreach ($ splits as $ step => $ split ){
$ Floor = $ step> 0? Floor ($ num/intval ($ step): '0 ';
If ($ floor> 0 ){
$ String [] = IntToCn ($ floor). $ split;
$ Num = fmod ($ num, $ step );
}
}
$ String2 = array ();
Foreach ($ chars as $ step => $ char ){
$ Floor = $ step> 0? Floor ($ num/intval ($ step): '0 ';
If ($ floor> 0 ){
$ String [] = $ string2 [] = $ ints [$ floor]. $ char;
$ Num = fmod ($ num, $ step );
} Else if (count ($ string2)> 0 | (count ($ string)> 0 & $ step! = '000000') & $ string2 [count ($ string)-1]! = $ Ints [0] & $ num> 0 ){
$ String [] = $ ints [0];
}
}
If (! Empty ($ dou )){
$ String = array_merge ($ string, array ('point'), $ dou );
}
Return join ('', $ string );
}
Test Results

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.