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

Hi,

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?

Thanks

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.

Hi,

Yes I know about the lack of distinction between intergers and floating point values in javascript …that’s why i asked 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

OR

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