卷胡猫 2019-03-18 12:26 采纳率: 100%
浏览 396
已采纳

使用了mattkang 大佬发布的数字阿拉伯转繁体中文的代码,发现有点问题,但是找不到出问题的地方。

因为是初次接触python,学了一点基础的应用就想自己做点东西玩,搬了mattkang 大佬写的代码自己又改了改,发现能用还挺开心,试了几个数字之后发现有问题:当输入的数字十位数为零时,返回的繁体金额会少一分钱。。。。求大佬解答问题出在哪里啊?还有其他的问题吗?
代码如下:

-*- coding: utf-8 -*-

#抄的mattkang大佬的代码(CSDN)
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 list(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])

n=float(input('请输入金额:',))
print(convert(n))

返回的结果例子
D:>python 金额繁体转换.py
请输入金额:2090104829.19
贰拾亿玖仟零壹拾万肆仟捌佰贰拾玖圆壹角玖分

D:>python 金额繁体转换.py
请输入金额:2345103.19
贰佰叁拾肆万伍仟壹佰零叁圆壹角捌分

D:>python 金额繁体转换.py
请输入金额:203.19
贰佰零叁圆壹角捌分

D:>python 金额繁体转换.py
请输入金额:203.17
贰佰零叁圆壹角陆分

  • 写回答

1条回答 默认 最新

  • 溪水人家 2019-03-18 16:03
    关注

    小数在计算机中用二进制表示时候,多数情况是不能精确的,是一个近似值。可以参考官方解释
    当然也可以运行一下下面代码,可以更好的了解。

    正确的方案:

    # -*- coding: utf-8 -*-
    """
     * Created by dxi on 3/18/2019
    """
    from fractions import Fraction
    from decimal import Decimal
    
    
    def show_decimal(n):
        """ show how decimal  be represented in computer """
    
        print("数字的二进制表示: {}".format(Fraction.from_float(n)))
        print("小数在计算机中实际大小为: {}".format(Decimal.from_float(n)))
    
    
    def convert(n):
        """convert money to chinese words"""
    
        units = ['', '万', '亿']
        nums = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']
        decimal_label = ['角', '分']
        small_int_label = ['', '拾', '佰', '仟']
    
        # 明白原理,可以删掉
        show_decimal(n)
    
        # 由于小数部分是一个近似值,这样分隔之后取前两位后导致错误。
        # int_part, decimal_part = str(int(n)), str(n - int(n))[2:]  # 分离整数和小数部分,
        # print(decimal_part)
    
        # 正确做法
        n = round(n, 2)
        int_part, decimal_part = str(n).split('.')
    
        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 list(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])
    
    n = float(input('请输入金额:',))
    print(convert(n))
    
    

    运行结果:

    请输入金额:203.19
    数字的二进制表示: 3574556282364887/17592186044416
    小数在计算机中实际大小为: 203.18999999999999772626324556767940521240234375

    贰佰零叁圆壹角玖分

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64