Skip to content

Instantly share code, notes, and snippets.

@ghengeveld
Created October 8, 2013 12:24
Show Gist options
  • Save ghengeveld/6883876 to your computer and use it in GitHub Desktop.
Save ghengeveld/6883876 to your computer and use it in GitHub Desktop.
Helper function, parses numeric input also ending with %
/**
* 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