Created
October 8, 2013 12:24
-
-
Save ghengeveld/6883876 to your computer and use it in GitHub Desktop.
Helper function, parses numeric input also ending with %
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
/** | |
* parseNumeric('123') === 123 | |
* parseNumeric('10 abc') === undefined | |
* parseNumeric('10.10%') === 10.1 | |
* parseNumeric(' 10.1000 ') === 10.1 | |
* parseNumeric('10. 01%' ) === undefined | |
* parseNumeric('10.0 %') === 10 | |
*/ | |
function parseNumeric(number) { | |
var num = ('' + number).trim(); | |
var reOut = /^(\d+\.?\d*)\s*%?$/.exec(num); | |
return reOut ? parseFloat(reOut[1]): undefined; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment