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.