-
-
Save hernandohhoyos/51713a4d8ecf916e90cc0c30c285a798 to your computer and use it in GitHub Desktop.
Fecha a letras (Python)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UNIDADES = ('cero','un','dos','tres','cuatro','cinco','séis','siete','ocho','nueve') | |
DECENAS = ('diez','once','doce','trece','catorce','quince','dieciseis','diecisiete','dieciocho','diecinueve') | |
DIEZ_DIEZ = ('cero','diez','veinte','treinta') | |
CIENTOS = ('_','ciento','doscientos','trescientos','cuatroscientos','quinientos','seiscientos','setecientos','ochocientos','novecientos') | |
def mes_a_letras(number=1): | |
html = "" | |
MESES = ('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre') | |
html += MESES[number-1] | |
return html | |
def leer_decenas(numero): | |
if numero < 10: | |
return UNIDADES[numero] | |
decena, unidad = divmod(numero, 10) | |
if numero <= 19: | |
resultado = DECENAS[unidad] | |
elif numero <= 29: | |
resultado = 'veinti%s' % UNIDADES[unidad] | |
else: | |
resultado = DIEZ_DIEZ[decena] | |
if unidad > 0: | |
resultado = '%s y %s' % (resultado, UNIDADES[unidad]) | |
return resultado | |
def leer_centenas(numero): | |
centena, decena = divmod(numero, 100) | |
if numero == 0: | |
resultado = 'cien' | |
else: | |
resultado = CIENTOS[centena] | |
if decena > 0: | |
resultado = '%s %s' % (resultado, leer_decenas(decena)) | |
return resultado | |
def leer_miles(numero): | |
millar, centena = divmod(numero, 1000) | |
resultado = '' | |
if (millar == 1): | |
resultado = '' | |
if (millar >= 2) and (millar <= 9): | |
resultado = UNIDADES[millar] | |
elif (millar >= 10) and (millar <= 99): | |
resultado = leer_decenas(millar) | |
elif (millar >= 100) and (millar <= 999): | |
resultado = leer_centenas(millar) | |
resultado = '%s mil' % resultado | |
if centena > 0: | |
resultado = '%s %s' % (resultado, leer_centenas(centena)) | |
return resultado | |
def numero_a_letras(numero_entero=0,has_number=False): | |
resultado = "" | |
if (numero_entero <= 99): | |
resultado = leer_decenas(numero_entero) | |
elif (numero_entero <= 999): | |
resultado = leer_centenas(numero_entero) | |
elif (numero_entero <= 999999): | |
resultado = leer_miles(numero_entero) | |
resultado = resultado.replace('uno mil', 'un mil') | |
resultado = resultado.strip() | |
resultado = resultado.replace(' _ ', ' ') | |
resultado = resultado.replace(' ', ' ') | |
if has_number: | |
resultado += " (" + str(numero_entero) + ")" | |
return resultado | |
def fecha_a_letras(dt,has_number=False): | |
html = "" | |
day = dt.day | |
month = dt.month | |
year = dt.year | |
if day == 1: | |
html = "al primer " | |
if has_number: | |
html += " (" + str(day) + ")" | |
html += " día" | |
else: | |
html = "a los " | |
html += numero_a_letras(day,has_number) | |
html += " días " | |
html += "del mes de " + mes_a_letras(month) | |
html += " de " + numero_a_letras(year,has_number) | |
return html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment