鱼C论坛

 找回密码
 立即注册
查看: 680|回复: 5

关于数字转换中的精度问题

[复制链接]
发表于 2025-2-18 12:59:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
以下是一段将数字转换为金额大写的Python代码,遇到一个特殊的问题,就是当小数部分是51的时候,会出现转换为伍角贰分或者伍角的错误,请大神指点。

  1. from decimal import Decimal, getcontext
  2. getcontext().prec = 50

  3. def num_to_rmb(num):
  4.     # 定义数字对应的大写汉字
  5.     num_dict = {0: '零', 1: '壹', 2: '贰', 3: '叁', 4: '肆', 5: '伍', 6: '陆', 7: '柒', 8: '捌', 9: '玖'}
  6.     # 定义单位
  7.     unit_dict = {0: '', 1: '拾', 2: '佰', 3: '仟', 4: '万', 8: '亿', 12: '万亿'}

  8.     # 分离整数部分和小数部分
  9.     num = Decimal(num)  #保证数值精度
  10.     num_str = str(num)
  11.     if '.' in num_str:
  12.         integer_part, decimal_part = num_str.split('.')
  13.         # 确保小数部分是两位长度
  14.         decimal_part = decimal_part.ljust(2, '0')
  15.     else:
  16.         integer_part = num_str
  17.         decimal_part = '00'

  18.     # 处理整数部分
  19.     integer_result = ""
  20.     if integer_part != "0":
  21.         length = len(integer_part)
  22.         group_count = (length + 3) // 4  # 计算有多少个 4 位一组
  23.         zero_between_groups = False
  24.         for i in range(group_count):
  25.             start = max(0, length - (i + 1) * 4)
  26.             end = length - i * 4
  27.             group = integer_part[start:end]
  28.             group_length = len(group)
  29.             group_result = ""
  30.             zero_flag = False
  31.             non_zero_digit_in_group = False
  32.             for j, digit in enumerate(group):
  33.                 digit = int(digit)
  34.                 if digit == 0:
  35.                     zero_flag = True
  36.                 else:
  37.                     non_zero_digit_in_group = True
  38.                     if zero_flag:
  39.                         group_result += "零"
  40.                         zero_flag = False
  41.                     group_result += num_dict[digit] + unit_dict[group_length - j - 1]

  42.             if group_result.endswith("零"):
  43.                 group_result = group_result.rstrip("零")

  44.             if non_zero_digit_in_group:
  45.                 if zero_between_groups and integer_result and integer_result[-1] != "零":
  46.                     integer_result += "零"
  47.                 integer_result = group_result + unit_dict[i * 4] + integer_result
  48.                 zero_between_groups = False
  49.             else:
  50.                 zero_between_groups = True

  51.         if integer_result.endswith("零"):
  52.             integer_result = integer_result.rstrip("零")

  53.     # 处理小数部分
  54.     decimal_result = ""
  55.     if decimal_part != "00":
  56.         jiao = int(decimal_part[0])
  57.         fen = int(decimal_part[1])
  58.         if jiao != 0:
  59.             decimal_result = num_dict[jiao] + "角"
  60.             if fen != 0:
  61.                 decimal_result += num_dict[fen] + "分"
  62.             else:
  63.                 decimal_result += "整"
  64.         elif fen != 0:
  65.             decimal_result = num_dict[fen] + "分"

  66.     # 拼接整数和小数部分结果
  67.     if decimal_result:
  68.         if integer_result:
  69.             integer_result += "元"
  70.             if integer_result[-1] != "零" and decimal_result and not decimal_result.startswith(num_dict[0]) and int(decimal_part[0]) == 0:
  71.                 result = integer_result + "零" + decimal_result
  72.             else:
  73.                 result = integer_result + decimal_result
  74.         else:
  75.             result = decimal_result
  76.     else:
  77.         if integer_result:
  78.             result = integer_result + "元整"
  79.         else:
  80.             result = "零元整"

  81.     return result

  82. print(num_to_rmb(123456789876543.51))
  83. print(num_to_rmb(10302450789002.51))        #输出错误???
  84. # print(num_to_rmb(1000010.01))
  85. # print(num_to_rmb(100.50))
  86. # print(num_to_rmb(10.50))
  87. # print(num_to_rmb(10.02))
  88. # print(num_to_rmb(10.00))
  89. # print(num_to_rmb(0.5))
  90. # print(num_to_rmb(0.12))
  91. # print(num_to_rmb(0.01))
  92. # print(num_to_rmb(0))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2025-2-18 13:09:10 | 显示全部楼层
以上代码复制粘贴到VS中后,格式不对,请问如何是好?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-2-18 13:15:36 | 显示全部楼层
将代码 num = Decimal(num).quantize(Decimal("0.01")) 中的0.01 改为 0.001,问题解决。谢谢!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-13 06:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表