3

First of all I am a beginner in C#, I have just started to play around with it as that's what my University course requires.

My problem is an assignment question which says:

h) To test if a number entered has an integer value. Hint: The number will have to be of type Double. If, for example, the number is 2.5 that doesn’t have an integer value but 2 does. You will need to use Convert.ToInt32(TheNumber) to convert the Double to an Int then compare the two.

double a, b, result;
            Console.WriteLine("Input a number");
            a = Convert.ToDouble(Console.ReadLine());
            b = Convert.ToInt32(a);

This is what I have at the moment and I don't know how to compare these 2 to test which one is an integer. I am pretty sure that you have to use an if statement but how to tell C# to test which of these 2 numbers is an integer and which one isn't!

Any help is highly appreciated :)

Jonathan
  • 648
  • 4
  • 13
  • 34
Przemek Wojtas
  • 1,311
  • 5
  • 26
  • 51
  • have you tried `a == b` - https://dotnetfiddle.net/6A3GwH? – Rhumborl Feb 07 '15 at 12:30
  • This is saying that if a is equal to b but they will never be equal as one number will be a decimal and another number a whole number which is a integer. I would need something like if I enter a = 2.5 it will say 2.5 is not an integer but then b = convert it to integer and print that 2 is an integer – Przemek Wojtas Feb 07 '15 at 12:35
  • `double a, b` - where is the integer? `2.00000` is the same as `2` as the fiddle shows (and everybody knows) – Rhumborl Feb 07 '15 at 12:37
  • The next line ask user to input a number and that is stored in a, and b basically converts a to integer – Przemek Wojtas Feb 07 '15 at 12:40
  • 1. Enter `string`; 2. Check it with `decimal.TryParse`; 3. If (2) is `false` then `Int32.TryParse`. – i486 Jan 31 '18 at 08:32

5 Answers5

5

Update:
I'd do it like this:

double d;
int i;
Console.WriteLine("Input a number");
d = Convert.ToDouble(Console.ReadLine());
i = Convert.ToInt32(d);
if(i == d) Console.WriteLine("It is an integral value");

This means: if you convert a double to an integer, it will lose all its digits after the decimal point. If this integer has the same value as the double, then the double had no digits after the decimal point, so it has an integer value.

DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • It's giving me an error message with line d = Convert.ToDouble(Console.ReadLine()) and I don't know why really – Przemek Wojtas Feb 07 '15 at 12:44
  • What is the error? It looks like the assignment lets you assume that the user enters an actual number, such as `2`, rather than something invalid like `gfjhsgfd`. – Rhumborl Feb 07 '15 at 12:52
  • when I input 2.5 for example i get An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll – Przemek Wojtas Feb 07 '15 at 12:56
  • how would I for example declare variable b to be any number from 1 to 100 (only whole numbers) and then if a is not equal to any of these numbers then round it up to nearest whole number maybe? – Przemek Wojtas Feb 07 '15 at 13:04
  • `If( i == d )` will not work, because it will implicitly convert i to double and then perform equality comparison between doubles, which **does not work**. See http://stackoverflow.com/q/17898266/773113 – Mike Nakis Feb 09 '15 at 10:43
  • @Mike Nakis Did you try it? It will work nicely in this case (small integers) – DrKoch Feb 09 '15 at 11:19
  • Hmm, yes, it works, the way I was thinking about it was flawed. – Mike Nakis Feb 09 '15 at 11:52
4

You can use TryParse method which returns boolean

        double mydouble;
        int myInt;
        string value = Console.ReadLine();
        if (double.TryParse(value, out mydouble))
        {
            //This is double value, you can perform your operations here
        }
        if (int.TryParse(value, out myInt))
        {
            //This is Int value, you can perform your operation here
        }
Haseeb Asif
  • 1,766
  • 2
  • 23
  • 41
  • 1
    It would be better, if you provide OP with sample code. – Farhad Jabiyev Feb 07 '15 at 12:28
  • I was writing , btw what is OP? – Haseeb Asif Feb 07 '15 at 12:29
  • 1
    OP = Original Poster. – Mike Nakis Feb 07 '15 at 12:32
  • 1
    You can look at [this](http://meta.stackexchange.com/questions/146513/what-does-op-mean). **The characters "OP" stand for "Original Poster", the person originally asking the question.** – Farhad Jabiyev Feb 07 '15 at 12:32
  • Haseeb, this will not work exactly as presented. `double.TryParse()` will succeed both for real and integer numbers, so the part where you say "//This is double value, you can perform your operations here" will execute always, both for real and for integer numbers, not only for real numbers. – Mike Nakis Feb 09 '15 at 10:41
0

Should be like this:

double d;
int i;      
Console.WriteLine("Input a number");
d = Convert.ToDouble(Console.ReadLine());
i = Convert.ToInt32(d);
if(i == d) Console.WriteLine("It is an integral value");
Hong
  • 1
0

I think you can use TryParse with do while loop

int  number;
string value;
do
{
  Console.Write("Enter a number : ");
  value =Console.ReadLine();

  if (!Int32.TryParse(value, out number))
       {
         Console.WriteLine("Wrong Input!!!!");
       }

}while (!Int32.TryParse(value, out number));
0

I would use '%' operator.

double number = 3.1;
bool isInteger = (number % 1) == 0;
string result = isInteger ? "" : "NOT ";
Console.WriteLine($"Number '{number}' is {result}integer.");
Tomas Paul
  • 490
  • 5
  • 13