Created
January 24, 2019 01:57
-
-
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)
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
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