Skip to content

Instantly share code, notes, and snippets.

@oHervis
Created June 24, 2021 20:16
Show Gist options
  • Save oHervis/2be621e63c079dcd44e2a58f98cddcda to your computer and use it in GitHub Desktop.
Save oHervis/2be621e63c079dcd44e2a58f98cddcda to your computer and use it in GitHub Desktop.
Função para formatar moeda a partir de qualquer string com valor
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