Created
October 1, 2013 20:09
-
-
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
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(){ | |
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