Example to explain how to convert numbers into uppercase letters in PHP

PHPz
Release: 2023-04-03 14:54:02
Original
674 people have browsed it

In PHP, you can use the following function to convert numbers into uppercase letters:

function num2upper($num) {
    $upper = array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");
    $unit = array("","拾","佰","仟","万","亿");
    $amount = "".round($num,2); //只保留两位小数
    $ystr = "点"; //小数点
    $len = strlen($amount);
    for ($i = $len - 1; $i >= 0; $i--) {
        $pos = $len - $i - 1;
        $num = $amount[$i];
        $chn = $upper[$num];
        if ($num == 0) {
            $unit_pos = $pos % 4;
            if ($unit_pos != 0) {
                $chn = "";
                if ($y_unit) {
                    $chn = $ystr;
                    $y_unit = false;
                }
                if ($pos == 4) {
                    $chn .= $unit[4];
                } elseif ($pos == 8) {
                    $chn .= $unit[5];
                    $y_unit = true;
                }
            }
        } else {
            $chn .= $unit[$pos % 4];
            $y_unit = false;
        }
        $upper_str = $chn . $upper_str;
    }
    return $upper_str;
}
Copy after login

Usage example:

$num = 1234567.89;
$upper = num2upper($num);
echo $upper; // 壹佰贰拾叁万肆仟伍佰陆拾柒元捌角玖分
Copy after login

When using this function, you only need to pass the number to be converted as a parameter Just pass it in. This function returns a string representing the converted uppercase amount. It should be noted that this function can only convert numbers into uppercase letters, not letters into numbers.

The above is the detailed content of Example to explain how to convert numbers into uppercase letters in PHP. For more information, please follow other related articles on the PHP Chinese website!

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