function int(a) {
return a - a === 0 && a.toString(32).indexOf('.') === -1
}
function float(a) {
return a - a === 0 && a.toString(32).indexOf('.') !== -1
}
You can add typeof a === 'number' if you want to exclude strings.
This maybe isn't as performant as the % answer, which prevents you from having to convert to a string first, but I haven't seen anyone post it yet, so here's another option that should work fine:
function isInteger(num) {
return num.toString().indexOf('.') === -1;
}
Here's my code. It checks to make sure it's not an empty string (which will otherwise pass) and then converts it to numeric format. Now, depending on whether you want '1.1' to be equal to 1.1, this may or may not be what you're looking for.
Any Float number with a zero decimal part (e.g. 1.0, 12.00, 0.0) are implicitly cast to Integer, so it is not possible to check if they are Float or not.
You can add
typeof a === 'number'
if you want to exclude strings.This maybe isn't as performant as the % answer, which prevents you from having to convert to a string first, but I haven't seen anyone post it yet, so here's another option that should work fine:
Here's my code. It checks to make sure it's not an empty string (which will otherwise pass) and then converts it to numeric format. Now, depending on whether you want '1.1' to be equal to 1.1, this may or may not be what you're looking for.
check for a remainder when dividing by 1:
If you don't know that the argument is a number you need two tests:
Any Float number with a zero decimal part (e.g. 1.0, 12.00, 0.0) are implicitly cast to Integer, so it is not possible to check if they are Float or not.