数字金额转换成中文大写金额的函数

/**
 * 数字金额转换成中文大写金额的函数
 * String Int  $num  要转换的小写数字或小写字符串
 * return 大写字母
 * 小数位为两位
 **/
function get_amount($num)
{
    $c1 = "零壹贰叁肆伍陆柒捌玖";
    $c2 = "分角元拾佰仟万拾佰仟亿";
    $num = round($num, 2);
    $num = $num * 100;
    if (strlen($num) > 10) {
        return "数据太长,没有这么大的钱吧,检查下";
    }
    $i = 0;
    $c = "";
    while (1) {
        if ($i == 0) {
            $n = substr($num, strlen($num) - 1, 1);
        } else {
            $n = $num % 10;
        }
        $p1 = substr($c1, 3 * $n, 3);
        $p2 = substr($c2, 3 * $i, 3);
        if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
            $c = $p1 . $p2 . $c;
        } else {
            $c = $p1 . $c;
        }
        $i = $i + 1;
        $num = $num / 10;
        $num = (int)$num;
        if ($num == 0) {
            break;
        }
    }
    $j = 0;
    $slen = strlen($c);
    while ($j < $slen) {
        $m = substr($c, $j, 6);
        if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
            $left = substr($c, 0, $j);
            $right = substr($c, $j + 3);
            $c = $left . $right;
            $j = $j - 3;
            $slen = $slen - 3;
        }
        $j = $j + 3;
    }
    if (substr($c, strlen($c) - 3, 3) == '零') {
        $c = substr($c, 0, strlen($c) - 3);
    }
    if (empty($c)) {
        return "零元整";
    } else {
        return $c . "整";
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是Python代码实现: ```python # encoding:utf-8 def transition(number): if not isinstance(number, (int, float)): return '参数类型错误' if number > 999999999999.99 or number < 0: return '金额范围有误' unit_list = ['千', '百', '十', ''] chinese_num_list = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'] chinese_unit_list = ['', '万', '亿', '兆'] integral, decimal = str(number).split('.') integral_list = list(integral) integral_list.reverse() unit_index, zero_num, chinese_num, chinese_money = 0, 0, 0, '' for i, num in enumerate(integral_list): if num == '0': zero_num += 1 if unit_index % 4 == 0: chinese_money = chinese_unit_list[unit_index // 4] + chinese_money if unit_index % 4 == 3: chinese_money = '零' + chinese_money else: if zero_num > 0: chinese_money = chinese_num_list[0] + chinese_money zero_num = 0 chinese_num = chinese_num_list[int(num)] chinese_money = chinese_num + unit_list[unit_index % 4] + chinese_money if unit_index % 4 == 3: chinese_money = chinese_unit_list[unit_index // 4] + chinese_money unit_index += 1 if zero_num > 0: chinese_money = chinese_num_list[0] + chinese_money chinese_money += '元' if decimal == '00': chinese_money += '整' else: chinese_money += chinese_num_list[int(decimal[0])] + '角' if decimal[1] != '0': chinese_money += chinese_num_list[int(decimal[1])] + '分' return chinese_money if __name__ == '__main__': number = 123456789.33 result = transition(number) print(result) ``` 这个函数可以将数字金额转换中文大写金额,例如将“123456789.33”转换为“壹亿贰仟叁佰肆拾伍万陆仟柒佰捌拾玖元叁角叁分”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值