When performing some data processing, we often encounter the need to convert lowercase numbers into uppercase numbers. In PHP, handling this situation is also very simple.
The specific processing method can be simply implemented through the following three steps.
Step 1: Define the uppercase characters corresponding to numbers
When converting lowercase numbers to uppercase numbers, you need to prepare a correspondence table to establish the numbers and corresponding uppercase characters so that Called in subsequent processing.
For example, the following is a corresponding table of numbers from 1 to 10 and Chinese uppercase numbers:
$cnNum = array( 1 => '壹', 2 => '贰', 3 => '叁', 4 => '肆', 5 => '伍', 6 => '陆', 7 => '柒', 8 => '捌', 9 => '玖', 10 => '拾' );
It should also be noted that the number "0" has no corresponding character in uppercase. Judgment and processing are required when handling.
Step 2: Represent the entered numbers as an array
In the process of converting lowercase numbers to uppercase numbers, we will need to decompose the entered numbers and convert each number is the corresponding uppercase character.
To do this, we need to convert the input number into an array for traversal processing.
//将输入的数字转变为一个数组 $nums = str_split($num); //对于数字 0 的处理 if($num == 0){ return '零'; }
Step 3: Traverse the array and convert each number into the corresponding uppercase character in turn
After converting the input number into an array and performing special processing, we can traverse the array, Convert each number to its corresponding uppercase character in turn in a loop.
//将数字转换为大写中文数字 $result = ''; foreach($nums as $key => $val){ $result .= $cnNum[$val]; if(count($nums) - 1 == $key){ //判断是否为最后一个数字 break; } $index = count($nums) - 1 - $key; //计算数字位数 $result .= $cnDigit[$index]; }
After the traversal is completed, we can get the result of converting lowercase numbers to uppercase numbers.
The specific code implementation is as follows:
function num2cnUpper($num){ //定义大写数字对应数组 $cnNum = array( 1 => '壹', 2 => '贰', 3 => '叁', 4 => '肆', 5 => '伍', 6 => '陆', 7 => '柒', 8 => '捌', 9 => '玖', 10 => '拾' ); //定义位数对应的数字 $cnDigit = array( 1 => '', 2 => '拾', 3 => '佰', 4 => '仟', 5 => '万', 6 => '拾万', 7 => '佰万', 8 => '仟万' ); //将输入的数字转变为一个数组 $nums = str_split($num); //对于数字 0 的处理 if($num == 0){ return '零'; } //将数字转换为大写中文数字 $result = ''; foreach($nums as $key => $val){ $result .= $cnNum[$val]; if(count($nums) - 1 == $key){ //判断是否为最后一个数字 break; } $index = count($nums) - 1 - $key; //计算数字位数 $result .= $cnDigit[$index]; } return $result; } //测试 echo num2cnUpper(2019); //输出:贰仟壹拾玖 echo num2cnUpper(0); //输出:零
In summary, the processing method of converting lowercase numbers to uppercase numbers in PHP is relatively simple. You need to prepare a corresponding table and use the Array representation, just traverse the array and convert each number into the corresponding uppercase character.
The above is the detailed content of How to convert lowercase numbers to uppercase in php. For more information, please follow other related articles on the PHP Chinese website!