- Home /
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;
}
Answer by Reder13 · Mar 25, 2015 at 03:51 PM
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
I am a bit lost, how would I put this number into context
I would do something like this:
float Number = 3;
// Use this for initialization void Start () {
print (Number % 2);
if ( Number % 2 != 0){ // if Number has a decimal
//dosomething;
}
you dont really need to have Number2 for your code since you know its going to be a 2
the code above prints out the remainder of Number and 2 (1 in this case) and since 1 isnt equal to 0 Number must be odd thus it enters the body of the if statement to do something.
@Reder13 the comment should say "if Number is odd" rather than "if Number has a decimal".
Yes and no in the current context yes your right it would be if Number is odd @Bonfire Boy. However I was thinking if Number were say 3.37 or some other value other than an int then it would be true if number has a decimal.
Thanks for re$$anonymous$$ding me to clarify that.
In the case you want to use Interger values only for Number just change the : float Numbers to int Numbers and the comment to // if Number is odd
@Reder13 I beg your pardon. The question talks about odd and even, so I had integers in my head and missed the fact that the code was all (strangely?) not using integers. Thanks for the clarification.
Answer by Bonfire-Boy · Mar 26, 2015 at 01:14 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613191225im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Casting-related error: "^ cannot be applied to double and int" 1 Answer
how to Break a vector3 line into half and Quater 2 Answers
How to move prefabs array in sine wave? 2 Answers