Finding out if a number is a whole number or a floating point value


does anyone know a good way to find out if a number is a whole number or a floating
point value?

I’ve thought that i can grab the number, convert it to a string and check the string
for a period…if it has a period then its a floating point number.

This method seems hacky to me and was wondering if there’s some other way of doing it?


To me the method you mentioned doesn’t look hackish at all. JavaScript, unlike other languages like PHP/C, does not differentiate between integer values and floating-point values. It knows only one numeric type: Number. So, you won’t find functions like is_int() or do something like if(getType(number) == ‘int’) in JavaScript. So, manually checking the presence of the decimal character is the only solution that comes to mind.


Yes I know about the lack of distinction between intergers and floating point values in javascript …that’s why i asked :slight_smile: Thanks for the clarification.

You can use the modulus operator.

For example 5 % 1 will return 0

However, 5.1 % 1 will return 0.1

so you could use

so if somevalue % 1 == 0 … then you know that somevalue is an integer otherwise it is fp

for testing type in 5%1 in google search to calculate the result. Then type in 5.1%1 in a google search to see that result. Try different combinations of numbers and you will understand what is going on.

hope this helps


<script type="text/javascript">
    var num = 12;
    if(parseInt(num) == num){
    else if(parseFloat(num) == num){

Is it the number you’re checking or the data type? Do you want 12.0 to be an integer or a float?

JavaScript doesn’t have integers or floats - just numbers so the data type either way is the same. That is why the original poster asked the question.

The %1 solution given would be the easiest most efficient way of calculating it as it doesn’t need a function call to work it out.

Sorry for the confusion. The question I was asking was whether the program needs to distinguish between numbers being entered as integers or floating point. Does it care if a user enters 12.0 as opposed to 12?