-
-
Save aVolpe/14c83273d2885720348dd791bb61c203 to your computer and use it in GitHub Desktop.
Código en JavaScript que convierte números a letras, bastante útil para formularios de documentos contables y similares
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
/*************************************************************/ | |
// NumeroALetras | |
// The MIT License (MIT) | |
// | |
// Copyright (c) 2015 Luis Alfredo Chee | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
// @author Rodolfo Carmona | |
// @contributor Jean ([email protected]) | |
// @contributor Arturo Volpe | |
/*************************************************************/ | |
const Unidades = (num: number) => { | |
switch (num) { | |
case 1: | |
return 'UN'; | |
case 2: | |
return 'DOS'; | |
case 3: | |
return 'TRES'; | |
case 4: | |
return 'CUATRO'; | |
case 5: | |
return 'CINCO'; | |
case 6: | |
return 'SEIS'; | |
case 7: | |
return 'SIETE'; | |
case 8: | |
return 'OCHO'; | |
case 9: | |
return 'NUEVE'; | |
default: | |
return ''; | |
} | |
}; | |
const Decenas = (num: number) => { | |
let decena = Math.floor(num / 10); | |
let unidad = num - (decena * 10); | |
switch (decena) { | |
case 1: | |
switch (unidad) { | |
case 0: | |
return 'DIEZ'; | |
case 1: | |
return 'ONCE'; | |
case 2: | |
return 'DOCE'; | |
case 3: | |
return 'TRECE'; | |
case 4: | |
return 'CATORCE'; | |
case 5: | |
return 'QUINCE'; | |
default: | |
return 'DIECI' + Unidades(unidad); | |
} | |
case 2: | |
switch (unidad) { | |
case 0: | |
return 'VEINTE'; | |
default: | |
return 'VEINTI' + Unidades(unidad); | |
} | |
case 3: | |
return DecenasY('TREINTA', unidad); | |
case 4: | |
return DecenasY('CUARENTA', unidad); | |
case 5: | |
return DecenasY('CINCUENTA', unidad); | |
case 6: | |
return DecenasY('SESENTA', unidad); | |
case 7: | |
return DecenasY('SETENTA', unidad); | |
case 8: | |
return DecenasY('OCHENTA', unidad); | |
case 9: | |
return DecenasY('NOVENTA', unidad); | |
case 0: | |
return Unidades(unidad); | |
} | |
}; | |
const DecenasY = (strSin: string, numUnidades: number) => numUnidades > 0 ? strSin + ' Y ' + Unidades(numUnidades) : strSin; | |
const Centenas = (num: number) => { | |
let centenas = Math.floor(num / 100); | |
let decenas = num - (centenas * 100); | |
switch (centenas) { | |
case 1: | |
if (decenas > 0) | |
return 'CIENTO ' + Decenas(decenas); | |
return 'CIEN'; | |
case 2: | |
return 'DOSCIENTOS ' + Decenas(decenas); | |
case 3: | |
return 'TRESCIENTOS ' + Decenas(decenas); | |
case 4: | |
return 'CUATROCIENTOS ' + Decenas(decenas); | |
case 5: | |
return 'QUINIENTOS ' + Decenas(decenas); | |
case 6: | |
return 'SEISCIENTOS ' + Decenas(decenas); | |
case 7: | |
return 'SETECIENTOS ' + Decenas(decenas); | |
case 8: | |
return 'OCHOCIENTOS ' + Decenas(decenas); | |
case 9: | |
return 'NOVECIENTOS ' + Decenas(decenas); | |
default: | |
return Decenas(decenas); | |
} | |
}; | |
const Seccion = (num: number, divisor: number, strSingular: string, strPlural: string) => { | |
let cientos = Math.floor(num / divisor); | |
let resto = num - (cientos * divisor); | |
let letras = ''; | |
if (cientos > 0) { | |
letras = cientos > 1 ? Centenas(cientos) + ' ' + strPlural : strSingular; | |
} else { | |
letras = strSingular; | |
} | |
if (resto > 0) { | |
letras += ''; | |
} | |
return letras; | |
}; | |
const Miles = (num: number) => { | |
let divisor = 1000; | |
let cientos = Math.floor(num / divisor); | |
let resto = num - (cientos * divisor); | |
let strMiles = Seccion(num, divisor, 'UN MIL', 'MIL'); | |
let strCentenas = Centenas(resto); | |
return strMiles == '' || cientos === 0 ? strCentenas : strMiles + ' ' + strCentenas; | |
}; | |
const Millones = (num: number) => { | |
let divisor = 1000000; | |
let cientos = Math.floor(num / divisor); | |
let resto = num - (cientos * divisor); | |
let strMillones = Seccion(num, divisor, millon(num, true), millon(num, false)); | |
let strMiles = Miles(resto); | |
return strMillones == '' || cientos === 0 ? strMiles : strMillones + ' ' + strMiles; | |
}; | |
const millon = (num: number, singular: boolean) => { | |
let letraMillon = singular ? 'UN MILLON' : 'MILLONES'; | |
if (num % 1000000 == 0) { | |
letraMillon = letraMillon + ' DE' | |
} | |
return letraMillon; | |
}; | |
export const GsAsString = (num: number) => { | |
return NumberAsString(num, false, { | |
plural: 'Guaranies', | |
singular: 'Guarni' | |
}) | |
}; | |
export const NumberAsString = (num: number, centavos: boolean = false, currency: { plural?: string, singular?: string, centPlural?: string, centSingular?: string } = {}) => { | |
currency = currency || {}; | |
let data = { | |
numero: num, | |
enteros: Math.floor(num), | |
centavos: centavos ? (((Math.round(num * 100)) - (Math.floor(num) * 100))) : 0, | |
letrasCentavos: '', | |
letrasMonedaPlural: currency.plural || 'PESOS', | |
letrasMonedaSingular: currency.singular || 'PESO', | |
letrasMonedaCentavoPlural: currency.centPlural || 'CENTAVOS', | |
letrasMonedaCentavoSingular: currency.centSingular || 'CENTAVO' | |
}; | |
if (data.centavos > 0) { | |
let centavos = data.centavos == 1 ? Millones(data.centavos) + ' ' + data.letrasMonedaCentavoSingular : Millones(data.centavos) + ' ' + data.letrasMonedaCentavoPlural; | |
data.letrasCentavos = 'CON ' + centavos; | |
} | |
if (data.enteros == 0) { | |
return 'CERO ' + data.letrasMonedaPlural + ' ' + data.letrasCentavos; | |
} | |
if (data.enteros == 1) { | |
return Millones(data.enteros) + ' ' + data.letrasMonedaSingular + ' ' + data.letrasCentavos; | |
} else { | |
return Millones(data.enteros) + ' ' + data.letrasMonedaPlural + ' ' + data.letrasCentavos; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment