snippetjavascriptCritical
How do I check that a number is float or integer?
Viewed 0 times
howcheckintegerfloatnumberthat
Problem
How to find that a number is
float or integer?1.25 --> float
1 --> integer
0 --> integer
0.25 --> floatSolution
check for a remainder when dividing by 1:
If you don't know that the argument is a number you need two tests:
Update 2019
5 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.
function isInt(n) {
return n % 1 === 0;
}If you don't know that the argument is a number you need two tests:
function isInt(n){
return Number(n) === n && n % 1 === 0;
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}Update 2019
5 years after this answer was written, a solution was standardized in ECMA Script 2015. That solution is covered in this answer.
Code Snippets
function isInt(n) {
return n % 1 === 0;
}function isInt(n){
return Number(n) === n && n % 1 === 0;
}
function isFloat(n){
return Number(n) === n && n % 1 !== 0;
}Context
Stack Overflow Q#3885817, score: 1516
Revisions (0)
No revisions yet.