-
-
Save jorgemvc/e89f69178de65c4a79f688802cc14eba to your computer and use it in GitHub Desktop.
Convertir de números a letras
This file contains 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
public static class NumerosLetras { | |
static string[] unidades = new string[] { | |
"cero", "un", "dos", "tres", "cuatro", "cinco", "seis", "siete", "ocho", "nueve", "diez", | |
"once", "doce", "trece", "catorce", "quince", "dieciseis", "diecisiete", "dieciocho", "diecinueve", "veinte" }; | |
static string[] decenas = new string[] { | |
"cero", "diez", "veinti", "treinta", "cuarenta", "cincuenta", "sesenta", "setenta", "ochenta", "noventa", "cien" }; | |
static string[] centenas = new string[] { | |
"cero", "ciento", "doscientos", "trescientos", "cuatrocientos", "quinientos", "seiscientos", | |
"setecientos", "ochecientos", "novecientos", "mil" }; | |
/// <summary> | |
/// Convertir un número a su equivalente en palabras | |
/// </summary> | |
/// <param name="numero">Número decimal a convertir</param> | |
/// <param name="moneda">Moneda a utilizar</param> | |
/// <param name="mayusculas">El resultado se expresa en mayúscula</param> | |
/// <param name="procesaDecimales">Se deben procesar los decimales</param> | |
/// <returns>La hilera correspondiente al numero</returns> | |
public static string Convertir( | |
decimal numero, | |
string moneda, | |
bool mayusculas = true, | |
bool procesaDecimales = true | |
) { | |
string letras = string.Empty; | |
string espacio; | |
int enteros = (int)Math.Floor(numero); | |
int decimales = (int)(Math.Round(numero - enteros, 2) * 100); | |
if (string.IsNullOrWhiteSpace(moneda)) { | |
throw new ArgumentNullException(nameof(moneda)); | |
} | |
if (enteros / 1_000_000 > 0) { | |
string textoMillones; | |
if (enteros / 1_000_000 == 1) { | |
textoMillones = "millón"; | |
} else { | |
textoMillones = "millones"; | |
} | |
letras = $"{Convertir(enteros / 1_000_000, moneda, mayusculas, procesaDecimales: false)} {textoMillones}"; | |
enteros %= 1_000_000; | |
} | |
if (enteros / 1_000 > 0) { | |
espacio = letras.Length > 0 ? " " : ""; | |
if (enteros / 1_000 == 1) { | |
letras += $"{espacio}{centenas[10]}"; | |
} else { | |
letras += $"{espacio}{Convertir(enteros / 1_000, moneda, mayusculas, procesaDecimales: false)} {centenas[10]}"; | |
} | |
enteros %= 1_000; | |
} | |
if (enteros / 100 > 0) { | |
espacio = letras.Length > 0 ? " " : ""; | |
if (enteros / 100 == 1 && enteros % 100 == 0) { | |
letras += $"{espacio}{decenas[10]}"; | |
} else { | |
letras += $"{espacio}{centenas[enteros / 100]}"; | |
} | |
enteros %= 100; | |
} | |
if (enteros <= 20) { | |
if (enteros > 0) { | |
espacio = (letras.Length > 0) ? " " : ""; | |
letras += $"{espacio}{unidades[enteros]}"; | |
} | |
} else { | |
espacio = letras.Length > 0 ? " " : ""; | |
if (enteros / 10 == 2) { | |
letras += $"{espacio}{decenas[enteros / 10]}{Convertir(enteros % 20, moneda, mayusculas, procesaDecimales: false)}"; | |
} else if (enteros % 10 == 0) { | |
letras += $"{espacio}{decenas[enteros / 10]}"; | |
} else { | |
letras += $"{espacio}{decenas[enteros / 10]} y {Convertir(enteros % 10, moneda, mayusculas, procesaDecimales: false)}"; | |
} | |
} | |
if (string.IsNullOrWhiteSpace(letras)) { | |
letras = "cero"; | |
} | |
if (procesaDecimales) { | |
letras += $" {moneda} con {decimales:00}/100"; ; | |
} | |
return mayusculas ? letras.ToUpper() : letras.ToLower(); | |
} // Convertir | |
} // class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment