Home > Backend Development > PHP Tutorial > PHP converts the number 1-100 million into Chinese characters, for example, 150 is converted into one hundred and fifty, 1-1150_PHP tutorial

PHP converts the number 1-100 million into Chinese characters, for example, 150 is converted into one hundred and fifty, 1-1150_PHP tutorial

WBOY
Release: 2016-07-13 09:46:13
Original
1178 people have browsed it

php Convert the number 1-100 million into Chinese characters, for example, 150 is converted into one hundred and fifty, 1-1150

Directly go to the example

Written over 100 billion.

/**

* @author jaSON
* Change the number 1-100 million into Chinese characters, such as: 123->one hundred and twenty-three
* @param [num] $num [number]
* @return [string] [string]
*/
function numToWord($num)
{
$chiNum = array('zero', 'one', 'two', 'three', 'four ', 'five', 'six', 'seven', 'eight', 'nine');
$chiUni = array('','ten', 'hundred', 'thousand', 'ten thousand', 'Billion', 'Ten', 'Hundred', 'Thousand');

$chiStr = '';


$num_str = (string)$num;

$count = strlen($num_str);
$last_flag = true; //Whether the previous one is 0
$zero_flag = true; //Whether the first one is
$temp_num = null; / /Temporary number

$chiStr = '';//Splicing result
if ($count == 2) {//Two 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);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1035428.htmlTechArticlephp Convert the numbers 1-100 million into Chinese characters, for example, 150 is converted into one hundred and fifty, 1-1150 Directly use examples to write hundreds of billions. /** * @author ja歌 * Change the number 1-100 million into Chinese characters, such as:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template