-3

Is there a simple expression I could put into an if statement to check if a value is an int? Something like this, to check if x is an int:

x = 1

if (Diceroll() = 1)
{
    x = x * 0.75
}
if (Diceroll() = 2)
{
    x = x * 1.25
}
if (Diceroll() = 3)
{
    x = x * 1
}


if (x != int)
{
    consol.writeline("X is not an int")
}
if (x = int)
{
    consol.writeline("X is an int")
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
  • 5
    Well what's the type of `x` to start with? Please give a [mcve]... we can't really tell what you're trying to achieve at the moment. – Jon Skeet Feb 27 '16 at 17:16
  • 1
    Your code won't compile as it. – John Alexiou Feb 27 '16 at 17:21
  • @ja72 I don't think you need this code to compile to see what it's supposed to do. – Sophie Coyne Feb 27 '16 at 17:22
  • I was referring to `consol` and `writeline` which are syntactically incorrect for no reason. – John Alexiou Feb 27 '16 at 17:34
  • I suggest you edit your title to something like "Is there a way to check if a decimal number is really an integer". – John Alexiou Feb 27 '16 at 17:38
  • @ja72 You know you can edit posts yourself, right? – Matti Virkkunen Feb 27 '16 at 17:50
  • 1
    Yes but how is this going to help the OP? It would be more helpful if their title matched what they were asking for more closely. – John Alexiou Feb 27 '16 at 17:57
  • When looking for the solution to a problem, you need to utilize the proper terminology. When we take your question literally, it is answered in http://stackoverflow.com/questions/983030/: `bool isInt = value is int;`. But that shows literally whether the value is an int, which is not what you seem to be looking for. Assuming that `x` is a floating-point number like `float` or `double`, the above check will never be true. Instead, your actual question is "how to check if a float contains an integer number", and that's been answered before in http://stackoverflow.com/questions/142252/. – CodeCaster Feb 27 '16 at 18:22
  • Does `Diceroll()` have different results on each call? What if it returns 1, 2, 3 - x is multiplied by 1.25 and 0.75 and 1! – Wai Ha Lee Feb 29 '16 at 00:20

4 Answers4

4

You can write something like this:

double x;

// your code here

if (x != Math.Floor(x))
   Console.WriteLine("X is not an int")
else
   Console.WriteLine("X is an int")

See also this answer: https://stackoverflow.com/a/1650120/3901618 about comparing floating point values (according to CodeCaster's comment).

Community
  • 1
  • 1
romanoza
  • 4,775
  • 3
  • 27
  • 44
  • 2
    Judging by the little information we have this is probably what OP was really looking for. Just have to be aware of rounding errors. Using a little epsilon value for a fuzzy comparison might be enough, but personally I'd avoid using floating point if possible. – Matti Virkkunen Feb 27 '16 at 17:25
  • 3
    Or simply x % 1 == 0, if doubles are his use case – Bogey Feb 27 '16 at 17:33
0

You may need to specify your question a little. First of all, what is the type of x - object?

Second, do you want to strictly test if its data type is int? That would be easy:

 object p = ....;
 bool is_int = p is int;

Or you want to check for addition things? For example...

  • ... the above would fail if p is an Int64, or An Uint32, etc. or basically any other integer type which isn't strictly int (Int32)
  • The above would also fail is for double p = 1.
  • ... And it would also fail for string p = "1"

These are cases you may or may not want to consider? If so, they need additional work.

Bogey
  • 4,926
  • 4
  • 32
  • 57
  • `is` works for reference types only. – John Alexiou Feb 27 '16 at 17:22
  • Hmm don't think so? bool is_int1 = ((int)1) is int; bool is_int2 = ((double)2) is int; compiles fine and works as expected – Bogey Feb 27 '16 at 17:25
  • I've always used `is` together with `as` which is definitely only for ref types. – John Alexiou Feb 27 '16 at 17:35
  • Yep, "as" only works with ref types. Everything else wouldn't make a lot of sense indeed; e.g. int number = somevariable as int; what should this return if somevariable wasn't int? default(int), so 0? That'd be indistinguishable from a a case where int somevariable = 0. Anyways, that's not an issue for the "is" keyword tho as that doesn't struggle with this kind of problem. – Bogey Feb 27 '16 at 17:53
0

There are two different interpretations of your question, if you're asking if the value of "x" can be an integer, you use.

int temp = 0;
string x = "numberofsomesort";
if (!int.TryParse(x,out temp))
{
    Console.Writeline("X is not an int");
}

Or if you have an object and you want to check its type, you use.

object x;

if (!(x is int))
{
    Console.Writeline("X is not an int");
}

EDIT: Looking at your new code, the first suggestion is the correct one as you're checking to see if the value of "x" can be converted into an integer.

Sophie Coyne
  • 1,018
  • 7
  • 16
0

You can do it in two ways:

int aIntValue;
if(int.TryParse(x, out aIntValue))

If int.TryParse returns true, it will convert the value 'x' to the varible "aIntValue'. If return false, the 'x' isn't a number.

Or you can do this:

if(!(x is int))
Thiago Ferreira
  • 661
  • 3
  • 19