Created
June 24, 2021 20:16
-
-
Save oHervis/2be621e63c079dcd44e2a58f98cddcda to your computer and use it in GitHub Desktop.
Função para formatar moeda a partir de qualquer string com valor
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
function formatCurrency(valor) { | |
const valorCagado = valor.replaceAll('.', '').replaceAll(',', '').replaceAll(/\D/g, '') | |
const valueWithoutZeros = parseFloat(valorCagado).toString() | |
arrValorCagado = valueWithoutZeros.split('') | |
arrValorCagado.splice(arrValorCagado.length - 2, 0, '.') | |
return Number(arrValorCagado.join('')).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment