Skip to content

Instantly share code, notes, and snippets.

@FranciscoG
Created October 1, 2013 20:09
Show Gist options
  • Save FranciscoG/6784335 to your computer and use it in GitHub Desktop.
Save FranciscoG/6784335 to your computer and use it in GitHub Desktop.
Find how many decimal places a number has. Also please note that I usually start coding these gists as Chrome Snippets so that's why they all are functions attached to window. You don't need to do that
(function(){
window.findDecimal = function(x){
if (typeof x === 'string') {
var x_str = parseFloat(x);
} else if (typeof x === 'number') {
var x_str = x.toString();
} else {
return false; //not a string or a number
}
var decimal_digits = x_str.length - x_str.lastIndexOf('.') - 1;
decimal_digits === x_str.length && (decimal_digits = 0); // case no decimal
return decimal_digits;
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment