You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python数字转英文金额文本函数的错误排查与修复请求

Python数字转英文金额文本函数的错误排查与修复请求

我正在实现一个用于支票金额格式转换的函数,需要把数字转换成对应的英文金额文本。具体需求如下:

实现函数number_to_text(value)-> str,输入一个小于1000000、最多保留两位小数的非负数值,返回对应的英文金额字符串。其中整数部分(对应美元)需要用英文单词完整拼写,小数部分(对应美分)格式为X/100;如果同时存在整数和小数部分,需要用and连接,比如1.20应返回'One and 20/100'。

我已经编写了以下代码,但运行时遇到了错误,部分测试用例的输出也不符合预期:

def number_to_text(value):
    # Define the words for numbers from 0 to 19
    words = {
        0: 'Zero', 1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five',
        6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten',
        11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen',
        15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen',
        19: 'Nineteen'
    }

    # Define the words for tens multiples
    tens_words = {
        20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty',
        60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety'
    }

    # Special case for zero
    if value == 0:
        return words[0]

    # Split the value into dollars and cents
    dollars = int(value)
    cents = int((value - dollars) * 100)

    dollars_text = ""
    if dollars > 0:
        thousands = dollars // 1000
        hundreds = dollars % 1000 // 100
        tens = dollars % 100 // 10
        ones = dollars % 10

        if thousands > 0:
            dollars_text += words[thousands] + "Thousand"
        if hundreds > 0:
            dollars_text += words[hundreds] + "Hundred"
        if tens > 1:
            dollars_text += tens_words[tens * 10] + " "
        if ones <= 10:
            dollars_text += words[ones] + " "
        elif tens == 1:
            dollars_text += words[tens * 10 + ones] + " "
        elif ones > 0:
            dollars_text += words[ones] + " "

        dollars_text = dollars_text.strip()

    cents_text = ""
    if cents > 0:
        cents_text = str(cents) + "/100"

    if dollars_text and cents_text:
        return dollars_text + " and " + cents_text
    if dollars_text:
        return dollars_text
    if cents_text:
        return cents_text
    else:
        return "Zero"

# 测试用例
print(number_to_text(10))
print(number_to_text(1.20))
print(number_to_text(999))
print(number_to_text(102))
print(number_to_text(0.23))
print(number_to_text(999999))

遇到的问题

  1. KeyError错误:当运行number_to_text(999999)时,触发了以下错误:
Traceback (most recent call last):
File "d:\UHasselt\Msc Stat&DS Year1\Programing with Python_Code\Assignment 5.py", line 64, in <module>
print(number_to_text(999999))  # Output: Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine
^^^^^^^^^^^^^^^^^^^^^^
File "d:\UHasselt\Msc Stat&DS Year1\Programing with Python_Code\Assignment 5.py", line 31, in number_to_text
dollars_text += words[thousands] + " Thousand "
~~~~~^^^^^^^^^^^
KeyError: 999
  1. 精度问题:输入1.20时,返回了One and 19/100,不符合预期的One and 20/100。

预期输出

我期望各个测试用例的输出如下:

  • number_to_text(10) → 'Ten'
  • number_to_text(1.20) → 'One and 20/100'
  • number_to_text(999) → 'Nine Hundred Ninety Nine'
  • number_to_text(102) → 'One Hundred Two'
  • number_to_text(0.23) → '23/100'
  • number_to_text(999999) → 'Nine Hundred Ninety Nine Thousand Nine Hundred Ninety Nine'

希望能帮忙排查并修复这些问题,让函数能正确处理所有符合要求的输入。

备注:内容来源于stack exchange,提问作者TANN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 09:41:19