# 四舍五入保留两位小数 def decimal_money(money): format_money = Decimal(money).quantize(Decimal("0.00"), rounding=ROUND_HALF_UP) return format_money # 将数字转中文大写 def num_to_cn_upper(amount): if int(amount) == 0: return "零元整" units = ['元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿'] digits = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] decimals = ['角', '分'] integer_part = int(amount) decimal_part = decimal_money(amount - integer_part) result = '' # 处理整数部分 integer_part_str = str(integer_part) for i, digit in enumerate(integer_part_str): if digit != '0': result += digits[int(digit)] result += units[len(integer_part_str) - i - 1] else: # 如果前一位不是零,则添加零 if i > 0 and integer_part_str[i - 1] != '0': result += digits[0] # 处理小数部分 if decimal_part > 0: decimal_part_str = str(decimal_part)[2:] for i, digit in enumerate(decimal_part_str): result += digits[int(digit)] result += decimals[i] else: result += "整" return result
标签:python,money,decimal,金额,大写,part,result,str,integer From: https://www.cnblogs.com/yzh2001/p/18360871