Was it helpful?

Question

How do I check that a number is float or integer - JavaScript?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

Let’s say we have the following variables −

var value1 = 10;
var value2 = 10.15;

Use the Number() condition to check that a number is float or integer −

Number(value) === value && value % 1 !== 0;
}

Example

Following is the code −

function checkNumberIfFloat(value) {
   return Number(value) === value && value % 1 !== 0;
}
var value1 = 10;
var value2 = 10.15;
if (checkNumberIfFloat(value1) == true)
   console.log("The value is float=" + value1);
else
   console.log("The value is not float=" + value1);
if (checkNumberIfFloat(value2) == true)
   console.log("The value is float=" + value2);
else
   console.log("The value is not float=" + value2);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo218.js.

Output

The output is as follows −

PS C:\Users\Amit\JavaScript-code> node demo218.js
The value is not float=10
The value is float=10.15
raja
Published on 03-Oct-2020 17:30:24
Advertisements
Was it helpful?
Not affiliated with Tutorialspoint
scroll top