Skip to content

Instantly share code, notes, and snippets.

@leodutra
Last active June 8, 2019 10:37
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/3057153 to your computer and use it in GitHub Desktop.
Save leodutra/3057153 to your computer and use it in GitHub Desktop.
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