Last active
January 15, 2025 22:00
-
-
Save Nick-Gabe/0dd98f7a5f814468593f05d18d894903 to your computer and use it in GitHub Desktop.
Um código que gera números por extenso
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
const pluralExtList = [ | |
{ | |
1: "um", | |
2: "dois", | |
3: "três", | |
4: "quatro", | |
5: "cinco", | |
6: "seis", | |
7: "sete", | |
8: "oito", | |
9: "nove", | |
}, | |
{ | |
2: "vinte", | |
3: "trinta", | |
4: "quarenta", | |
5: "cinquenta", | |
6: "sessenta", | |
7: "setenta", | |
8: "oitenta", | |
9: "noventa", | |
}, | |
{ | |
1: "cento", | |
2: 'duzentos', | |
3: 'trezentos', | |
4: 'quatrocentos', | |
5: 'quinhentos', | |
6: 'seiscentos', | |
7: 'setecentos', | |
8: 'oitocentos', | |
9: 'novecentos', | |
} | |
] | |
const regularExtList = [ | |
{ | |
1: "um", | |
2: "dois", | |
3: "três", | |
4: "quatro", | |
5: "cinco", | |
6: "seis", | |
7: "sete", | |
8: "oito", | |
9: "nove", | |
}, | |
{ | |
2: "vinte", | |
3: "trinta", | |
4: "quarenta", | |
5: "cinquenta", | |
6: "sessenta", | |
7: "setenta", | |
8: "oitenta", | |
9: "noventa", | |
}, | |
{ | |
1: "cem", | |
2: 'duzentos', | |
3: 'trezentos', | |
4: 'quatrocentos', | |
5: 'quinhentos', | |
6: 'seiscentos', | |
7: 'setecentos', | |
8: 'oitocentos', | |
9: 'novecentos', | |
} | |
] | |
const irregular = { | |
0: "zero", | |
10: "dez", | |
11: "onze", | |
12: "doze", | |
13: "treze", | |
14: "quatorze", | |
15: "quinze", | |
16: "dezesseis", | |
17: "dezessete", | |
18: "dezoito", | |
19: "dezenove", | |
}; | |
const housesExt = [['', ''], ['mil', 'mil'], ['milhão', 'milhões']] | |
function getNumberInWords(num) { | |
const irregularNumber = irregular[num] | |
if (irregularNumber) return irregularNumber | |
const blocks = num | |
.toString() | |
.split('') | |
.reverse() | |
.join('') | |
.match(/[0-9]{0,3}/g) | |
.map(x => x.split('').reverse()) | |
.slice(0, -1) | |
const result = blocks | |
.map((houses, index) => { | |
if (houses.length < 3) { | |
houses.reverse().push('0', '0') | |
houses = houses.slice(0, 3).reverse() | |
} | |
return houses | |
.map((num, i) => { | |
const lastTwoChars = houses.slice(-2).join('') | |
if (irregular[lastTwoChars] && i === 1) return irregular[lastTwoChars] | |
else if (irregular[lastTwoChars] && i === 2) return '' | |
if (lastTwoChars !== '00' && i === 0) { | |
return num == 0 ? null : pluralExtList[2 - i][num] | |
} | |
return num == 0 ? null : regularExtList[2 - i][num] | |
}) | |
.filter(x => x) | |
.join(' e ') | |
+ ` ${housesExt[index][1]}` | |
}) | |
.reverse() | |
.join(', ') | |
return result | |
} | |
console.log('1.', getNumberInWords(123456789)); | |
console.log('2.', getNumberInWords(12)); | |
console.log('3.', getNumberInWords(99999)); | |
console.log('4.', getNumberInWords(0)); | |
console.log('5.', getNumberInWords(5728)); | |
console.log('6.', getNumberInWords(100)); | |
console.log('6.', getNumberInWords(1000000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment