0

有没有办法在不丢失前导零的情况下将数字、字符串中的 ether 或 int 转换为单词?我正在尝试将日期和时间以及电话号码转换为单词,但是第二次我将字符串值转换为 int 我丢失了前导零。

这是我现在对单词进行数字计算的代码,只要没有前导零,它就可以很好地工作。这是我的问题的一个例子......假设我正在转动一个日期 08-02-2004 我不希望这个输出为零八零二......等等,但我要在当前状态下这样做我会必须做一些关于方法的事情......除非我错过了一些东西。

units = ["", "one", "two", "three", "four",  "five", 
    "six", "seven", "eight", "nine "]
teens = ["", "eleven", "twelve", "thirteen",  "fourteen", 
    "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"]
tens = ["", "ten", "twenty", "thirty", "forty",
    "fifty", "sixty", "seventy", "eighty", "ninety"]
thousands = ["","thousand", "million",  "billion",  "trillion", 
    "quadrillion",  "quintillion",  "sextillion",  "septillion", "octillion", 
    "nonillion",  "decillion",  "undecillion",  "duodecillion",  "tredecillion", 
    "quattuordecillion",  "sexdecillion",  "septendecillion",  "octodecillion", 
    "novemdecillion",  "vigintillion "]

def numToWords(self, num):
    words = []
    if num == 0:
        words.append("zero")
    else:
        numStr = "%d" % num
        numStrLen = len(numStr)
        groups = (numStrLen + 2) / 3
        numStr = numStr.zfill(groups * 3)
        for i in range(0, groups*3, 3):
            h = int(numStr[i])
            t = int(numStr[i+1])
            u = int(numStr[i+2])
            g = groups - (i / 3 + 1)

            if h >= 1:
                words.append(units[h])
                words.append("hundred")

            if t > 1:
                words.append(tens[t])
                if u >= 1:
                    words.append(units[u])
            elif t == 1:
                if u >= 1:
                    words.append(teens[u])
                else:
                    words.append(tens[t])
            else:
                if u >= 1:
                    words.append(units[u])

            if g >= 1 and (h + t + u) > 0:
                words.append(thousands[g])
    return ' '.join([w for w in words]) 

对此的任何帮助或建议将不胜感激。

4

3 回答 3

1

确保您首先提供了一个字符串,并且仅在需要时才评估为 int 。例如:

def numToWords(self, numStr):
    words = []
    if int(numStr) == 0:
        words.append("zero")
    else:
        # no longer needed, it's already a string
        # numStr = "%d" % num
        numStrLen = len(numStr)
        groups = (numStrLen + 2) /
        ...
于 2013-07-19T00:11:24.083 回答
1

当您使用 %d 将 int 格式化为字符串时,它会丢弃所有前导零。要保留它们,您需要指定最小位数,如下所示:

numStr = "%03d" % num

这会将前导零附加到任何少于 3 位的数字(在这种情况下,最小位数为 3)。但是,在你随便打前导零之前,你首先需要决定你想看到多少个总数字。

于 2013-07-19T00:16:22.077 回答
0

这个使用递归方法的解决方案怎么样?

def numToWords(i):
    if i < 20:
        result = 'zero,one,two,three,four,five,six,\
                  seven,eight,nine,ten,eleven,twelve,\
                  thirteen,fourteen,fifteen,sixteen,\
                  seventeen,eighteen,nineteen'.split(',')[i]
    elif i < 100:
        result = ',,twenty,thirty,forty,fifty,sixty,seventy,\
                    eighty,ninety'.split(',')[i//10]
        if i % 10:
            result += ' ' + numToWords(i % 10)
    elif i < 1000:
        result = checkio(i // 100) + ' hundred'
        if i % 100:
            result += ' ' + numToWords(i % 100)
    return result
于 2013-12-17T12:33:03.160 回答