zoukankan      html  css  js  c++  java
  • 阿拉伯数字金额转中文大写 (python实现)

    分析
    转载:https://blog.csdn.net/u010180339/article/details/52563487
    分小数和整数部分进行处理
    末尾的零应舍弃
    中间有连续多个零,只取一个零
    整数部分从右往左以4位为步长扫描

    实现
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals

    def convert(n):
        units = ['', '万', '亿']
        nums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
        decimal_label = ['角', '分']
        small_int_label = ['', '拾', '佰', '仟']
        int_part, decimal_part = str(int(n)), str(n - int(n))[2:]  # 分离整数和小数部分
        res = []
        if decimal_part:
            res.append(''.join([nums[int(x)] + y for x, y in zip(decimal_part, decimal_label) if x != '0']))
        if int_part != '0':
            res.append('圆')
            while int_part:
                small_int_part, int_part = int_part[-4:], int_part[:-4]
                tmp = ''.join([nums[int(x)] + (y if x != '0' else '') for x, y in zip(small_int_part[::-1], small_int_label)[::-1]])
                tmp = tmp.rstrip('零').replace('零零零', '零').replace('零零', '零')
                unit = units.pop(0)
                if tmp:
                    tmp += unit
                    res.append(tmp)
        return ''.join(res[::-1])

    print convert(0.22)
    print convert(0.20)
    print convert(0.02)
    print convert(1)
    print convert(12)
    print convert(123)
    print convert(1234)
    print convert(1230)
    print convert(1204)
    print convert(1034)
    print convert(1004)
    print convert(51234)
    print convert(51204)
    print convert(51034)
    print convert(50234)
    print convert(50034)
    print convert(50004)
    print convert(50000)
    print convert(12351234)
    print convert(12301234)
    print convert(12351234)
    print convert(12051234)
    print convert(10351234)
    print convert(10051234)
    print convert(10001234)
    print convert(10000000)
    print convert(10000004)
    print convert(10000030)
    print convert(10000200)
    print convert(10001000)
    print convert(10050000)
    print convert(12000010001)
    print convert(1409.50)
    123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
    输出:
    贰角贰分
    贰角
    贰分
    壹圆
    壹拾贰圆
    壹佰贰拾叁圆
    壹仟贰佰叁拾肆圆
    壹仟贰佰叁拾圆
    壹仟贰佰零肆圆
    壹仟零叁拾肆圆
    壹仟零肆圆
    伍万壹仟贰佰叁拾肆圆
    伍万壹仟贰佰零肆圆
    伍万壹仟零叁拾肆圆
    伍万零贰佰叁拾肆圆
    伍万零叁拾肆圆
    伍万零肆圆
    伍万圆
    壹仟贰佰叁拾伍万壹仟贰佰叁拾肆圆
    壹仟贰佰叁拾万壹仟贰佰叁拾肆圆
    壹仟贰佰叁拾伍万壹仟贰佰叁拾肆圆
    壹仟贰佰零伍万壹仟贰佰叁拾肆圆
    壹仟零叁拾伍万壹仟贰佰叁拾肆圆
    壹仟零伍万壹仟贰佰叁拾肆圆
    壹仟万壹仟贰佰叁拾肆圆
    壹仟万圆
    壹仟万零肆圆
    壹仟万零叁拾圆
    壹仟万零贰佰圆
    壹仟万壹仟圆
    壹仟零伍万圆
    壹佰贰拾亿零壹万零壹圆
    壹仟肆佰零玖圆伍角
    ————————————————
    版权声明:本文为CSDN博主「mattkang」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/handsomekang/article/details/52563487
  • 相关阅读:
    安装jar包到本地仓库和远程仓库
    服务之间的资源权限校验
    函数指针
    malloc分配内存
    cuda_vs_报错无法解析的外部错误
    c语言读写文件
    C++使用using namespace std报错分析与解决方案
    MPI环境配置
    c语言学习
    openMP
  • 原文地址:https://www.cnblogs.com/yuany66/p/11970491.html
Copyright © 2011-2022 走看看