Skip to content

Instantly share code, notes, and snippets.

@rutcreate
Created March 20, 2016 08:03
Show Gist options
  • Save rutcreate/03ff3f9bd5f414465322 to your computer and use it in GitHub Desktop.
Save rutcreate/03ff3f9bd5f414465322 to your computer and use it in GitHub Desktop.
JavaScript: Integer and Float validation
function isInt(n){
return Number(n).toString() === n.toString() && n % 1 === 0;
}
function isFloat(n){
return Number(n).toString() === n.toString() && n % 1 !== 0;
}
@rutcreate
Copy link
Author

// Usage of isInt()
isInt("10");
// true
isInt("123.99");
// false
isInt(10);
// true
isInt(123.99);
// false
isInt("this is text");
// false
// Usage of isFloat()
isFloat("10");
// false
isFloat("123.99");
// true
isFloat(10);
// false
isFloat(123.99);
// true
isFloat("this is text");
// false```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment