How to find that a number is float or integer?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
How to find that a number is float or integer?
1.25 --> float
1 --> integer
0 --> integer
0.25 --> float
How about this one?
isFloat(num) {
return typeof num === "number" && !Number.isInteger(num);
}
We can check by isInteger function.
ie number will return true and float return false
console.log(Number.isInteger(2)),<BR>Will return true
console.log(Number.isInteger(2.5))Will return false
try this
let n;
return (n = value % 1) !== 0 && !isNaN(n);
when the return value is false means the input value is float number or float string, otherwise the input value is integer numbef or integer string.
basically it needs to check the precision value for not equal to zero.
another one is to check the correct string number also.
const integerCheck = (num) => {
const isInt = (n) => Number(n) === n && n % 1 === 0
const isFloat = (n) => Number(n) === n && n % 1 !== 0
return (isInt(num) || !isFloat(num))
}
console.log( integerCheck('23.3') );
try this one
function amountcheck()
{
var dpamt=$('#dpamt').val()/5000;
var ints=dpamt.toString();
var isint=ints.split('.');
if(isint[1]>0)
{
alert('float value');
return false;
}
else
{
alert('int value');
}
}
You can use the Number.isInteger() method to check if the number is an integer or a float by dividing them for example:
function isNumberFloatOrInteger(a, b){
if(Number.isInteger(a / b)){
return true;
}
else{ return false };
}
Note: isInteger() is not compatible with internet explorer.
There is Number.isInteger(number) to check this. Doesn't work in Internet Explorer but that browser isn't used anymore. If you need string like "90" to be an integer (which wasnt the question) try Number.isInteger(Number(number)). The "official" isInteger considers 9.0 as an integer, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number. It looks like most answers are correct for older browsers but modern browsers have moved on and actually support float integer check.
For Float
var decimal= /^[-+]?[0-9]+\.[0-9]+$/;
if (!price.match(decimal)) {
alert('Please enter valid float');
return false;
}
For integer
var number = /^\d+$/;
if (!price.match(number)) {
alert('Please enter valid integer');
return false;
}
With this you can check if a string or a number is "decimal" (float properly):
var IsDecimal = function(num){
return ((num.toString().split('.').length) <= 2 && num.toString().match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/)) ? (!isNaN(Number.parseFloat(num))) : false ;
}
and this other works for check if a string or a number is Integer:
var IsInteger = function(num){
return ((num.toString().split('.').length) == 1 && num.toString().match(/^[\-]?\d+$/)) ? (!isNaN(Number.parseInt(num))) : false ;
}
var IsDecimal = function(num){
return ((num.toString().split('.').length) <= 2 && num.toString().match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/)) ? (!isNaN(Number.parseFloat(num))) : false ;
}
var IsInteger = function(num){
return ((num.toString().split('.').length) == 1 && num.toString().match(/^[\-]?\d+$/)) ? (!isNaN(Number.parseInt(num))) : false ;
}
console.log("-------------- As string --------------");
console.log("Integers:");
console.log("0 = " + IsInteger("0"));
console.log("34 = " + IsInteger("34"));
console.log(".34 = " + IsInteger(".34"));
console.log("3.4 = " + IsInteger("3.4"));
console.log("3e = " + IsInteger("3e"));
console.log("e3 = " + IsInteger("e3"));
console.log("-34 = " + IsInteger("-34"));
console.log("--34 = " + IsInteger("--34"));
console.log("034 = " + IsInteger("034"));
console.log("0-34 = " + IsInteger("0-34"));
console.log("Floats/decimals:");
console.log("0 = " + IsDecimal("0"));
console.log("64 = " + IsDecimal("64"));
console.log(".64 = " + IsDecimal(".64"));
console.log("6.4 = " + IsDecimal("6.4"));
console.log("6e2 = " + IsDecimal("6e2"));
console.log("6e = " + IsDecimal("6e"));
console.log("e6 = " + IsDecimal("e6"));
console.log("-64 = " + IsDecimal("-64"));
console.log("--64 = " + IsDecimal("--64"));
console.log("064 = " + IsDecimal("064"));
console.log("0-64 = " + IsDecimal("0-64"));
console.log("\n-------------- As numbers --------------");
console.log("Integers:");
console.log("0 = " + IsInteger(0));
console.log("34 = " + IsInteger(34));
console.log(".34 = " + IsInteger(0.34));
console.log("3.4 = " + IsInteger(3.4));
console.log("-34 = " + IsInteger(-34));
console.log("034 = " + IsInteger(034));
console.log("0-34 = " + IsInteger(0-34));
console.log("Floats/decimals:");
console.log("0 = " + IsDecimal(0));
console.log("64 = " + IsDecimal(64));
console.log(".64 = " + IsDecimal(0.64));
console.log("6.4 = " + IsDecimal(6.4));
console.log("6e2 = " + IsDecimal(6e2));
console.log("-64 = " + IsDecimal(-64));
console.log("064 = " + IsDecimal(064));
console.log("0-64 = " + IsDecimal(0-64));
to check the number is Int or not and apply 2 decimal format, you can use the formula below in React-Native.
isInt = (n) => {
return n % 1 === 0;
}
show = (x) => {
if(x) {
if (this.isInt(x)) {
return ${x}
}
else {
return ${x.toFixed(2)}
}
}
}
2022 update - We could simply use the Number's methods.
Check if integer or float :
Number.isFinite(val)
Check if integer :
Number.isInteger(val)
Check if float (not integer) : !Number.isInteger(val) && Number.isFinite(val)
I know there are 30 answers already, but one complicated way is to do this:
function isInteger(n) {
return n.toString().split('.').length === 1;
}
Explanation: We first convert n to a string, and split it based on a dot. If n is a floating point, like 4.5, then split will return an array ['4', '5']. If it is an integer like 45, it will return ['45']. Therefore, if the length of the array is 1, then we know it is a number.
P.S. If you want to write this function in the new ES6 format (arrow functions):
const isInteger = n => n.toString().split('.').length === 1;
This is the best solution I can come up for float and interger number checking.
function isFloat(n) {
if (!n) {
return false
}
return !isNaN(n % 1) && n % 1 !== 0;
}
function isInt(n) {
if (n.length==0) {
return false
}
return !isNaN(n % 1) && n % 1 == 0;
}
I'm late to the party but here is my version
isInteger: obj => Number.isInteger(!isNaN(obj % 1) && obj % 1 !== 0 ? obj : parseInt(obj)),
Another method is:
function isFloat(float) {
return /\./.test(float.toString());
}
Might not be as efficient as the others but another method all the same.