Skip to content

Instantly share code, notes, and snippets.

@cyrilf
Created December 21, 2012 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyrilf/4356185 to your computer and use it in GitHub Desktop.
Save cyrilf/4356185 to your computer and use it in GitHub Desktop.
A little improvement of parseFloat function to handle string in ending with %.
var f = getNumber('23.7'); // f = 23.7
//var f = getNumber(23.7); // f = 23.7
//var f = getNumber('23.7 px'); // f = 23.7
//var f = getNumber('23.7 %'); // f = 0.237
function getNumber(input) {
if( typeof input === 'string' && input.slice(-1) === '%') {
input = parseFloat(input.slice(0,-1))/100;
} else {
input = parseFloat(input);
}
return input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment