0

I have this program that changes a number into words, but the problem is that when you have more than 16 digits in the number, it starts changing the numbers. This is done in Python 3 and we are wondering why this is happening and if there is a simple workaround? (We suspect that Python only handles 16 significant digits) Of course it could be a silly bug and in which case, I apologise... But we've had 3 sets of eyes on this and can't spot anything.

Any help is much appreciated.

digit1 = ["","one","two","three","four","five","six","seven","eight","nine"]
digit2 = ["","twenty","thirty","fourty","fifty","sixty","seventy","eighty","ninety"]
other = ["","thousand","million","billion","trillion","quadrillion","quintillion","sextillion","septillion","octillion","nonillion","decillion","undecillion","duodecillion","tredecillion","quattuordecillion","quindecillion","sexdecillion","septendecillion","octodecillion","novemdecillion","vigintillion","centillion"]
teens = ["","eleven","twelve","thirteen","fourteen","fifteen","sixteen","eighteen","nineteen"]
text = ""


number = input("Enter number: ")
try:
    number = float(number)
except ValueError:
    print ("That's not a number")
    exit()
decimal = number
number = int(number)


def Num2Text(num):
    out = ""
    if len(num) >= 3:
        out += digit1[int(num[0])] + " hundred "
    if len(num) >= 2 and num[-2] != "1":
        out += digit2[int(num[-2])-1] + " "
    if len(num) >= 1 and num[-2] != "1":
        out += digit1[int(num[-1])] + " "
    if num[-2] == "1":
        out += teens[int(num[-1])] + " "
    return out


number = str(number)
cycles = len(number)/3
cycles = round(cycles+0.49)

for i in range(cycles,0,-1):
    cnumber = str(int(decimal/1000**(i-1)))
    cnumber = cnumber[-3:]
    text += Num2Text(cnumber)
    text += other[i-1]  + " "


print(text)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
James Abela
  • 97
  • 1
  • 3

0 Answers0