# How to detect if number has a decimal point or not

Hi I am currently trying to find out weather a number is odd or even, to do this I have divided it by two and want to see if it has a decimal point or not. The problem is i’m not 100% sure how I would achieve that.

My code is bellow if i am not explaining it well enough.

using UnityEngine;
using System.Collections;

public class Math : MonoBehaviour {

``````decimal Number = 3;
decimal number2 = 2;

// Use this for initialization
void Start () {
Number = Number / number2;
print (Number);
if (Number Has a decimal Point){
dosomething;

}
``````

There are actually two questions here.

“Odd” and “even” are concepts that only apply to integers.

One way to test whether a number is odd or even is as Reder13
says, to use the mod operator. But it’s only when you’re dealing
with integers, that you can do it with a single test…

``````EParity OddOrEven( int i )
{
return ( i % 2 == 0 )?( EParity.Even ):( EParity.Odd );
}
``````

But note that this only works because for integers, i%2
can only take the values 1 and 0.

Alternatively, and probably faster, you could just look at
the ones bit

``````EParity OddOrEven2( int i )
{
return ( ( i & 1 ) == 0 )?( EParity.Even ):( EParity.Odd );
}
``````

But you’re using decimals in your code, so there’s a third
possibility and you need to do 2 tests.

``````EParity OddOrEven( float f )
{
if ( f % 2 == 0 )
return EParity.Even;
else if ( f % 2 == 1 )
return EParity.Odd;
return EParity.Neither;
}
``````

The bitwise version only works with integers so to use
that with floats you need to check if it’s an integer first…

``````EParity OddOrEven2( float f )
{
int i = Mathf.FloorToInt( f );
if ( f != float( i ) )
return EParity.Neither;
return OddOrEven2( i );
}
``````

…the first part of which is a way of doing what the
question in the title asks for. Whether or not a float
has anything after the decimal point is the same as asking
if it’s (not) an integer, and you can do that by casting
back and forth then comparing.

you need to use the mod operator %

this gives you the remainder of two numbers

so number % number2 == 0 if it is even
and number % number2 != 0 means odd