2013-07-19 68 views
0

有沒有辦法在不丟失前導零的情況下將字符串中的數字,或者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]) 

任何幫助或建議,將不勝感激。

+0

你想要多少個前導零? –

回答

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)/
     ... 
+0

感謝這對我來說是一個巨大的幫助......我只需要弄清楚有多少個前導零就是通過簡單的計算得到的:這裏是代碼。 'numStrLen = len(numStr) numintStr = len(str(int(numStr))) #如果thre是一個差異,那麼這就是我需要追加多少個前導零。 DIFF = numStrLen - numintStr 如果DIFF> 0: 對於i在範圍(差異): words.append( '零')' 工作就像一個魅力。再次非常感謝你!我替換了if int(numStr)== 0並將其替換爲上面的代碼。 –

1

當您使用%d將int格式化爲字符串時,它將丟棄任何前導零。爲了留住他們,你需要指定數字的最小數目,像這樣:

numStr = "%03d" % num 

這將導致零追加到任意數量的具有不超過3位(製作的數字3的最小數量,在這種情況下, )。但是在你無條件地敲打前導零之前,首先需要確定你想看到多少個總位數。

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