# Check if a number has a decimal place/is a whole number

I am looking for an easy way in JavaScript to check if a number has a decimal place in it (in order to determine if it is an integer). For instance,

``````23 -> OK
5 -> OK
3.5 -> not OK
34.345 -> not OK
``````
``if(number is integer) {...}`` ``````Number.isInteger(23);  // true
Number.isInteger(1.5); // false
Number.isInteger("x"); // false:
``````

Number.isInteger() is part of the ES6 standard and not supported in IE11.

It returns false for `NaN`, `Infinity` and non-numeric arguments while `x % 1 != 0` returns true. Or you could just use this to find out if it is NOT a decimal:

``````string.indexOf(".") == -1;
`````` The most common solution is to strip the integer portion of the number and compare it to zero like so:

``````function Test()
{
var startVal = 123.456
alert( (startVal - Math.floor(startVal)) != 0 )
}
`````` Simple, but effective!

``````Math.floor(number) === number;
`````` ``````Number.prototype.isInt= function(){
return this== this>> 0;
}
``````

I always feel kind of bad for bit operators in javascript-

they hardly get any exercise. ``````var re=/^-?[0-9]+\$/;
var num=10;
re.test(num);
`````` ``````number = 20.5

if (number == Math.floor(number)) {

} else {

}
``````

Pretty cool and works for things like XX.0 too! It works because Math.floor() chops off any decimal if it has one so if the floor is different from the original number we know it is a decimal! And no string conversions :) `Number.isInteger()` is probably the most concise. It returns true if it is an integer, and false if it isn't. ``````parseInt(num) === num
``````

when passed a number, `parseInt()` just returns the number as int:

``````parseInt(3.3) === 3.3 // false because 3 !== 3.3
parseInt(3) === 3     // true
`````` convert number string to array, split by decimal point. Then, if the array has only one value, that means no decimal in string.

``````if(!number.split(".")){
//do stuff
}
``````

This way you can also know what the integer and decimal actually are. a more advanced example would be.

``````number_to_array = string.split(".");
inte = number_to_array;
dece = number_to_array;

if(!dece){
//do stuff
}
`````` Here's an excerpt from my guard library (inspired by Effective JavaScript by David Herman):

``````var guard = {

guard: function(x) {
if (!this.test(x)) {
throw new TypeError("expected " + this);
}
}

// ...
};

// ...

var number = Object.create(guard);
number.test = function(x) {
return typeof x === "number" || x instanceof Number;
};
number.toString = function() {
return "number";
};

var uint32 = Object.create(guard);
uint32.test = function(x) {
return typeof x === "number" && x === (x >>> 0);
};
uint32.toString = function() {
return "uint32";
};

var decimal = Object.create(guard);
decimal.test = function(x) {
return number.test(x) && !uint32.test(x);
};
decimal.toString = function() {
return "decimal";
};

uint32.guard(1234);     // fine
uint32.guard(123.4);    // TypeError: expected uint32

decimal.guard(1234);    // TypeError: expected decimal
decimal.guard(123.4);   // fine
`````` You can multiply it by 10 and then do a "modulo" operation/divison with 10, and check if result of that two operations is zero. Result of that two operations will give you first digit after the decimal point. If result is equal to zero then the number is a whole number.

``````if ( (int)(number * 10.0) % 10 == 0 ){
}
`````` Function for check number is Decimal or whole number

``````function IsDecimalExist(p_decimalNumber) {
var l_boolIsExist = true;

if (p_decimalNumber % 1 == 0)
l_boolIsExist = false;

return l_boolIsExist;
}
`````` You can use the bitwise operations that do not change the value (`^ 0` or `~~`) to discard the decimal part, which can be used for rounding. After rounding the number, it is compared to the original value:

``````function isDecimal(num) {
return (num ^ 0) !== num;
}

console.log( isDecimal(1) ); // false
console.log( isDecimal(1.5) ); // true
console.log( isDecimal(-0.5) ); // true
`````` Perhaps this works for you?

It uses regex to check if there is a comma in the number, and if there is not, then it will add the comma and stripe.

``````var myNumber = '50';
if(/,/.test(text) == false){
return text += ',-';
} else {
return text;
}
}
`````` Using modulus will work:

``````num % 1 != 0
// 23 % 1 = 0
// 23.5 % 1 = 0.5
``````

Note that this is based on the numerical value of the number, regardless of format. It treats numerical strings containing whole numbers with a fixed decimal point the same as integers:

``````'10.0' % 1; // returns 0
10 % 1; // returns 0
'10.5' % 1; // returns 0.5
10.5 % 1; // returns 0.5
`````` ``````function isDecimal(n){
if(n == "")
return false;

var strCheck = "0123456789";
var i;

for(i in n){
if(strCheck.indexOf(n[i]) == -1)
return false;
}
return true;
}
`````` You can use the following function to check if a number has decimal places:

``````function hasDecimal(num){
return !!(num % 1);
}

console.log(hasDecimal(2)); //true
console.log(hasDecimal(2.32423)); //false
``````

So apparently some users want explanations. I'll break it into pieces: (num % 1)

Parenthesis mean perform those operations first. num - a variable passed in by the function. % - a modulus symbol which attempts to divide the left number by the right. If there is a remainder it returns it as a decimal. If it divides evenly it returns 0.

So, sum up what we have so far., (num % 1) will return:

0 when divided evenly OR #.##### if not

0 == false.

[ANY_NUMBER_NOT_ZERO] == true.

Examples: new Boolean(0) is false new Boolean(12312.1231) is true

Other alternatives: A user could have tried to return the value of (num % 1)., which would basically achieve the same thing. Since return (0) is false and return (1.213113) is true.

But I wanted to return a boolean value. So as a shortcut to force a value into a boolean I added a ! symbol before it.

As many as might know., ! means NOT. But it also forces a value into a boolean.

Since the ! symobol forces the value into a boolean and it inverses its value. I use !! to force the value into a boolean and invert its value back to its original boolean value. ``````function isDecimal(num) {
return (num !== parseInt(num, 10));
}
`````` ``````function isWholeNumber(num) {
return num === Math.round(num);
}
`````` Use following if value is string (e.g. from `<input`):

``````Math.floor(value).toString() !== value
``````

I add `.toString()` to floor to make it work also for cases when `value == "1."` (ends with decimal separator or another string). Also `Math.floor` always returns some value so `.toString()` never fails.