Home > Backend Development > PHP Tutorial > How to convert numeric amounts to uppercase amounts in PHP (code example)

How to convert numeric amounts to uppercase amounts in PHP (code example)

不言
Release: 2023-04-05 13:12:02
forward
3696 people have browsed it

The content of this article is about how to convert digital amounts to uppercase amounts in PHP (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

An amount conversion function was used in CRM in the early years. I took it out from the old project and recorded it today. There are many functional methods for converting amounts, and they are all very good. However, this is a conversion function written by Xiao Cuigang when he was working, so it is somewhat commemorative. If there are any problems, please point them out and Xiao Cui will correct them in time. Thank you!

Without further ado, let’s get straight to the code:

<?php
/**
* 数字金额转换大写数字
* $num 数字类型
*/
function inttodaxie($num) {
    //判断$num是否数字
    if(!is_numeric($num)) return -1;
    $dint = array(&#39;零&#39;, &#39;壹&#39;, &#39;贰&#39;, &#39;叁&#39;, &#39;肆&#39;, &#39;伍&#39;, &#39;陆&#39;, &#39;柒&#39;, &#39;捌&#39;, &#39;玖&#39;);
    $len = strlen($num);
    $dstr = &#39;&#39;;
    for($i = 0; $i <= $len; $i++) {
        $key_ = substr($num, $i, 1);
        $dstr .= $dint[$key_];
    }
    return $dstr;
}

$result = inttodaxie(90011234);
var_dump($result);
echo &#39;<br>&#39;;
Copy after login

The above is the basic conversion code. On this basis, a secondary transformation is carried out:

<?php
/**
* 转换大写金额
* $num 数字类型
*/
function inttod($num) {
    //判断$num是否存在
    if(!$num) return &#39;零圆&#39;;
    //保留小数点后两位
    $num = round($num, 2);
    //将浮点转换为整数
    $tem_num = $num * 100;
    //判断数字长度
    $tem_num_len = strlen($tem_num);
    if($tem_num_len > 14) {
        return &#39;数字太大了吧,有这么大的金钱吗&#39;;
    }
    //大写数字
    $dint = array(&#39;零&#39;, &#39;壹&#39;, &#39;贰&#39;, &#39;叁&#39;, &#39;肆&#39;, &#39;伍&#39;, &#39;陆&#39;, &#39;柒&#39;, &#39;捌&#39;, &#39;玖&#39;);
    //大写金额单位
    $danwei = array(&#39;仟&#39;, &#39;佰&#39;, &#39;拾&#39;, &#39;亿&#39;, &#39;仟&#39;, &#39;佰&#39;, &#39;拾&#39;, &#39;万&#39;, &#39;仟&#39;, &#39;佰&#39;, &#39;拾&#39;, &#39;圆&#39;);
    $danwei1 = array(&#39;角&#39;, &#39;分&#39;);
    //空的变量用来保存转换字符串
    $daxie = &#39;&#39;;
    //分割数字,区分圆角分
    list($left_num, $right_num) = explode(&#39;.&#39;, $num);
    //计算单位长度
    $danwei_len = count($danwei);
    //计算分割后的字符串长度
    $left_num_len = strlen($left_num);
    $right_num_len = strlen($right_num);
    //循环计算亿万元等
    for($i = 0; $i < $left_num_len; $i++) {
        //循环单个文字
        $key_ = substr($left_num, $i, 1);
        //判断数字不等于0或数字等于0与金额单位为亿、万、圆,就返回完整单位的字符串
        if($key_ !== &#39;0&#39; || ($key_ == &#39;0&#39; && ($danwei[$danwei_len - $left_num_len + $i] == &#39;亿&#39; || $danwei[$danwei_len - $left_num_len + $i] == &#39;万&#39; || $danwei[$danwei_len - $left_num_len + $i] == &#39;圆&#39;))) {
            $daxie = $daxie . $dint[$key_] . $danwei[$danwei_len - $left_num_len + $i];
        } else {
            //否则就不含单位
            $daxie = $daxie . $dint[$key_];
        }
    }
    //循环计算角分
    for($i = 0; $i < $right_num_len; $i++) {
        $key_ = substr($right_num, $i, 1);
        if($key_ > 0) {
            $daxie = $daxie . $dint[$key_] . $danwei1[$i];
        }
    }
    //计算转换后的长度
    $daxie_len = strlen($daxie);
    //设置文字切片从0开始,utf-8汉字占3个字符
    $j = 0;
    while($daxie_len > 0) {
        //每次切片两个汉字
        $str = substr($daxie, $j, 6);
        //判断切片后的文字不等于零万、零圆、零亿、零零
        if($str == &#39;零万&#39; || $str == &#39;零圆&#39; || $str == &#39;零亿&#39; || $str == &#39;零零&#39;) {
            //重新切片
            $left = substr($daxie, 0, $j);
            $right = substr($daxie, $j + 3);
            $daxie = $left . $right;
        }
        $j += 3;
        $daxie_len -= 3;
    }
    return &#39;人民币&#39; . $daxie . &#39;整&#39;;
}
$result = inttod(99090909090.19);
var_dump($result);
Copy after login

Running results:

How to convert numeric amounts to uppercase amounts in PHP (code example)

The source code has been uploaded to GitHub: https://github.com/cuiyuanxin/php-demo/blob/master/capital.php

The above is the detailed content of How to convert numeric amounts to uppercase amounts in PHP (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:cnblogs.com
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