user3289670
user3289670

Reputation: 5

How can I define comparison has to be an int?

Here a short code for the thing which is already coded:

if (str.Length % 3 != 0)

Now my question is, how can I define, that if str.Length % 3 = int it has to do something?

Here an example:

123456789123 / 3 = int...

I know, the Syntax I used isn't correct, but it's because I don't know how to do it. You would also help me, if you told me, what the "opposite" of if (str.Length % 3 != 0) is.

Thanks for helping.

Upvotes: 0

Views: 77

Answers (3)

BenVlodgi
BenVlodgi

Reputation: 2185

It will be an int no matter what with the code you have provided.

Reason: int / int = int ... any decimal values will be truncated (not rounded). C# does not automatically turn numbers into float or double if there is need for it. You need to do type conversion of that nature explicitly.

I think you may have also confused modulo % and divide /. If you want to know if there is no remainder which means that the number coming out of the computation is an Integer do the if (str.Length % 3 != 0) you put in the code.... I assume you're looking for something like this

if (str.Length % 3 != 0)
{
    int num = str.Length / 3;
    //Now do something with your int version of num
}
else
{
    double num = str.Length / (double)3;
    //Now do something with your double version of num
}

By casting 3 which is an int to double the resulting number will be a double, if you don't do that you will get a truncated integer value then implicitly casted to a double and stored in num.

Upvotes: 2

walther
walther

Reputation: 13600

The statement str.Length % 3 always results in an integer. What you need is probably just a simple negation of this statement, that will tell you, that there is a remainder...

Negation of != is of course ==

Upvotes: 1

Udit Narayan
Udit Narayan

Reputation: 109

try this

if ((str.Length % 3).GetType() == typeof(int))
{
        //is integer
}

Upvotes: -1

Related Questions