Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active June 8, 2019 10:37
Embed
What would you like to do?
A parseNumber accepting: currency / decimal / float / whatever number ( for JavaScript )
function parseNumber(str)
{
str = (str + '').replace(/[^\d,.-]/g, '') // just digits, separators and sign
var sign = str.charAt(0) === '-' ? '-' : '+' // store sign
var minor = str.match(/[.,](\d+)$/) // filter decimals
str = str.replace(/[.,]\d*$/, '').replace(/\D/g, '') // remove decimals and any integer separator
return Number(sign + str + (minor ? '.' + minor[1] : '')) // build number
}
@leodutra
Copy link
Author

Stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment