我是 python 的初学者程序员,我需要一些代码方面的帮助。附件是我一直在处理的代码。目的是取一个像 305.67 这样的美元金额,并将该数字转换为像“305 美元和 67 美分”这样的文本短语。到目前为止,我已经获得了将其分解为文本的大部分代码,但我仍然对数字 11-19 存在问题,这是一种特殊情况。我需要帮助弄清楚程序如何决定在何处正确应用 11-19,以及何时删除不需要的“ZERO”字符串。如果你运行这个程序,你就会明白我的意思。主函数中的下面的循环将根据您希望的函数运行循环。此外,还有一个名为“getDollarFormatText”的函数,它将获取货币的数字版本,例如 305.67 或 45。13并给你它的文本格式。我遇到的问题是如何让程序忽略小数点,然后将所有内容适当地转换为小数点的左侧和右侧。这是一个加载的问题,我将彻底理解这一点。基本上这个问题很容易解决,但我不知道如何解决它。代码以处理两位数字的函数开头(我已经处理了仅由一位数字分隔的函数)。
def getWordForTwoDigits(amount):
#This value uses integer division to get the number of tens within a number.
tensAmount = int(amount) / 10
#This variable uses the first function above to find the equivalent single number within the number given using modulo. (Zero through 9)
singlesWord = getWordForDigit(int(amount)%10)
#For this decision structure, the structure is set to figuring out the number of tens within a number and appropriately naming it after its correct name (ten, twenty, etc.)
if tensAmount == 1:
wordTen = "TEN"
else:
if tensAmount == 2:
wordTen = "TWENTY"
else:
if tensAmount == 3:
wordTen = "THIRTY"
else:
if tensAmount == 4:
wordTen = "FORTY"
else:
if tensAmount == 5:
wordTen = "FIFTY"
else:
if tensAmount == 6:
wordTen = "SIXTY"
else:
if tensAmount == 7:
wordTen = "SEVENTY"
else:
if tensAmount == 8:
wordTen = "EIGHTY"
else:
if tensAmount == 9:
wordTen = "NINETY"
return "%s-%s"%(wordTen, singlesWord)
########################################
def getWordForThreeDigits(dolamount):
hundredAmount = int(dolamount) / 100
twoDigits = getWordForTwoDigits(int(dolamount) % 100)
if hundredAmount == 0:
return twoDigits
else:
if hundredAmount == 1:
wordHun = "ONE HUNDRED"
else:
if hundredAmount == 2:
wordHun = "TWO HUNDRED"
else:
if hundredAmount == 3:
wordHun = "THREE HUNDRED"
else:
if hundredAmount == 4:
wordHun = "FOUR HUNDRED"
else:
if hundredAmount == 5:
wordHun = "FIVE HUNDRED"
else:
if hundredAmount == 6:
wordHun = "SIX HUNDRED"
else:
if hundredAmount == 7:
wordHun = "SEVEN HUNDRED"
else:
if hundredAmount == 8:
wordHun = "EIGHT HUNDRED"
else:
if hundredAmount == 9:
wordHun = "NINE HUNDRED"
return "%s %s"%(wordHun, twoDigits)
####################################
def getDollarFormatText(dollarAndCents):
#how would you separate 190.67 (example) to 190 and 67 and and give the text form for eacn