Implementamos tecnologia y es asi como lo hacemos

Convertir numeros y decimales a letras Python

Escrito el Octubre 16, 2008 a las 12:43 PM por Camilo Nova

Luego de un post anterior sobre convertir numeros a letras en python me ha llegado una modificacion de Ulfang que les presento a continuacion:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
UNIDADES = (
   '',
   'UN ',
   'DOS ',
   'TRES ',
   'CUATRO ',
   'CINCO ',
   'SEIS ',
   'SIETE ',
   'OCHO ',
   'NUEVE ',
   'DIEZ ',
   'ONCE ',
   'DOCE ',
   'TRECE ',
   'CATORCE ',
   'QUINCE ',
   'DIECISEIS ',
   'DIECISIETE ',
   'DIECIOCHO ',
   'DIECINUEVE ',
   'VEINTE '
)
DECENAS = (
   'VENTI',
   'TREINTA ',
   'CUARENTA ',
   'CINCUENTA ',
   'SESENTA ',
   'SETENTA ',
   'OCHENTA ',
   'NOVENTA ',
   'CIEN '
)
CENTENAS = (
   'CIENTO ',
   'DOSCIENTOS ',
   'TRESCIENTOS ',
   'CUATROCIENTOS ',
   'QUINIENTOS ',
   'SEISCIENTOS ',
   'SETECIENTOS ',
   'OCHOCIENTOS ',
   'NOVECIENTOS '
)
 
def toWord(number_in, pesos=True):
 
   """
   Converts a number into string representation
   """
   converted = ''
 
   if type(number_in)  'str':
       number = str(number_in)
   else:
       number = number_in
 
   number = number.replace(",","")
   try:
       number_int, number_dec = number.split(".")
   except ValueError:
       number_int = number
       number_dec = ""
 
   if not (0 < __convertStr(number_int)  0):
           converted += '%sMILLONES ' % __convertNumber(millones)
 
   if(miles):
       if(miles == '001'):
           converted += 'MIL '
       elif(int(miles) > 0):
           converted += '%sMIL ' % __convertNumber(miles)
 
   if(cientos):
       if(cientos == '001'):
           converted += 'UN'
       elif(int(cientos) > 0):
           converted += '%s' % __convertNumber(cientos)
 
   if pesos:
       if number_dec == "":
           number_dec = "00"
       converted += 'PESOS ' + number_dec + "/100 M.N."
   else:
       if number_dec  "":
           converted +=  'PUNTO ' + toWord(number_dec,False)
 
   return converted.title()
 
def __convertNumber(n):
   """
   Max length must be 3 digits
   """
   output = ''
 
   if(n == '100'):
       output = "CIEN "
   elif(n[0] != '0'):
       output = CENTENAS[int(n[0])-1]
 
   k = int(n[1:])
   if(k  30) & (n[2] != '0')):
           output += '%sY %s' % (DECENAS[int(n[1])-2], UNIDADES[int(n[2])])
       else:
           output += '%s%s' % (DECENAS[int(n[1])-2], UNIDADES[int(n[2])])
 
   return output
 
def __convertStr(s):
   """Convert string to either int or float."""
   try:
       ret = int(s)
   except ValueError:
       #Try float.
       ret = float(s)
   return ret

Leave a Reply

AxiaCore Blog

Publicidad

Seguimiento

Etiquetas

Nosotros Leemos

Comentarios Recientes:

  • William: Hola. Muy buenos los apuntes. Estoy en el proceso de autoestudio con miras a la certificación CX-310-065 y...
  • None: Intenté utilizar el código para convertir un tipo “Double” (con centavos) a la representación en...
  • los harris: una página para niños y niñas para menores de 18 años, que haya mogollones de juegos divertidisimos y...
  • natali: bueno yo quiero saber como abrir mi propia pagina para que todos la vean ok…
  • hum: A mi se me queda sonando el bucle de audio bloqueado aunque reinicie asi…

Enlaces Recientes:

Archivo

Admin