Skip to content

Instantly share code, notes, and snippets.

@lucasdu4rte
Created January 24, 2019 01:57
Show Gist options
  • Save lucasdu4rte/d86152f136759e6e0e452035c554d3d7 to your computer and use it in GitHub Desktop.
Save lucasdu4rte/d86152f136759e6e0e452035c554d3d7 to your computer and use it in GitHub Desktop.
Converter string com formato de moeda para float (Ex: 'R$ 1.000,00' => 1000.00)
function currencyToFloat (currency = '') {
const regex = /([+-]?[0-9|^.|^,]+)[\.|,]([0-9]+)$/gim
const result = regex.exec(currency)
const floatResult = result
? result[1].replace(/[.,]/g, '') + '.' + result[2]
: currency.replace(/[^0-9-+]/g, '')
return floatResult
}
module.exports = currencyToFloat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment