covert number into string

Jan Ulrich Hasecke juhasecke at googlemail.com
Thu Jan 21 06:48:28 EST 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 21.01.10 12:18, anusha k wrote:
> Hi,
> 
> Can anyone tell me how to convert number to words
> For example:
> If number =9999 then it should give me *Nine thousand nine hundred
> ninetynine*
> Is there any build-in function or something that can do this for me
> Thank you in advance
> Anusha Kadambala
> 

Hey thats like an exercise in the wonderful German book "Python for Kids".

Should be pretty easy to implement it by yourself.

Maybe you can understand the code without the german comments.

But keep in mind that this is an excercise for kids in chapter 5. I am
sure there are better implementations.

# Python für Kids -- 3. Auflage, Kapitel 5
# Autor: Gregor Lingl
# Datum: 01. 03. 2004
# Loesung von Kapitel 5, Aufgabe 3:

def zahlwort(zahl):
    einer = zahl % 10  # Einerstelle: Rest bei der Division von zahl
durch 10
    if einer == 1: e = "ein"
    elif einer == 2: e = "zwei"
    elif einer == 3: e = "drei"
    elif einer == 4: e = "vier"
    elif einer == 5: e = "fünf"
    elif einer == 6: e = "sechs"
    elif einer == 7: e = "sieben"
    elif einer == 8: e = "acht"
    elif einer == 9: e = "neun"

    zehner = zahl // 10  # Zehnerstelle: Ergebnis der Ganzzahldivision
                         # von zahl durch 10
    if zehner == 0:   z = ""
    elif zehner == 1: z = "zehn"
    elif zehner == 2: z = "zwanzig"
    elif zehner == 3: z = "dreissig"
    elif zehner == 4: z = "vierzig"
    elif zehner == 5: z = "fünfzig"
    elif zehner == 6: z = "sechzig"
    elif zehner == 7: z = "siebzig"
    elif zehner == 8: z = "achtzig"
    elif zehner == 9: z ="neunzig"

    if zahl == 11:    print "elf"
    elif zahl == 12:  print "zwölf"
    elif zahl == 17:  print "siebzehn"
    elif einer == 0:  print z
    elif zehner == 1: print e+z
    else:             print e+"und"+z

zahl = raw_input("Gib eine zweistellige ganze Zahl ein: ")
zahl = int(zahl)  # macht aus dem String zahl eine ganze Zahl
zahlwort(zahl)
print

Cheers!
juh
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAktYPwwACgkQPUzUEFbILMRLWwCgjugY7Wxzec7AH8Esz1hNIf8d
WgoAoKgFo59NoBVk0jGZsqe5slOc7P9W
=cP1x
-----END PGP SIGNATURE-----




More information about the Python-list mailing list