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

PHP converts the numbers 100-100 million into Chinese characters, for example, 150 is converted into one hundred and fifty

WBOY
WBOYOriginal
2016-08-08 09:20:33968browse

Go directly to the example

Write it to 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', 'hundred million', '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 it is the first one
$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);

The above introduces PHP to convert the numbers 100-100 million into Chinese characters, for example, 150 is converted into one hundred and fifty, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:PHP programmer?Next article:PHP programmer?