Last active
June 8, 2019 10:37
-
-
Save leodutra/3057153 to your computer and use it in GitHub Desktop.
A parseNumber accepting: currency / decimal / float / whatever number ( for JavaScript )
This file contains 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 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stable.