Home > Blockchain >  I have a problem converting int to double
I have a problem converting int to double

Time:04-13

I'm a beginner programmer and at this moment i try to create primitive program for define variable types. Below code and core of my problem.

namespace Moving
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            
            if (int.TryParse(input, out int result1))
            {
                Console.WriteLine("type int");
            }
            if (double.TryParse(input, out double result2))
            {
                Console.WriteLine("type double");
            }
            else
            {
                Console.WriteLine("type String");
            }
            Console.ReadLine();
        }
    }
}

When i use string or double it works normally. But when i input int, works first if{} and second if{} too. For example i input 12. Program writes "type int" and "type double" because int may convert to double without efforts. I don't need it. Can i don't convert variable int to double? And i haven't a clue how i can explain to program to see difference between "string" and "char". What may do with it?

CodePudding user response:

Well, you can try else if instead of just if. Please, note, that every int can be parsed as double and every char can be treated as string:

if (int.TryParse(input, out int result1))
    Console.WriteLine("type int");
else if (double.TryParse(input, out double result2))
    Console.WriteLine("type double");
else if (input.Length == 1) // char is a string of Length == 1
    Console.WriteLine("type char");
else
    Console.WriteLine("type String");

CodePudding user response:

In your statements, there are two different condition blocks. Therefore, you will get two outputs. If you only want to get one output. You should use one condition block: If, Else If, Else.

        string input = Console.ReadLine();
        
        if (int.TryParse(input, out int result1))
        {
            Console.WriteLine("type int");
        }
        else if (double.TryParse(input, out double result2))
        {
            Console.WriteLine("type double");
        }
        else
        {
            Console.WriteLine("type String");
        }
        Console.ReadLine();

CodePudding user response:

Because (mathematically) 1 = 1.0 is true, knowing this we derive that all integers are also valid decimal numbers, in programming terms we call them floating point numbers, and double is a floating point number type, meaning, it represents a floating point number, and as such can also represent integers*. So technically there's no problem, but it's not behaving how we want it to behave, so how do we fix this?

Easy, we use an else if statement instead of an if statement. The else if is only checked if the if (or else if) before it is false. Implementing it looks like this:

string input = Console.ReadLine();

if (int.TryParse(input, out int intResult))
{
    Console.WriteLine("Integer");
}
else if (double.TryParse(input, out double doubleResult))
{
    Console.WriteLine("Double");
}
else
{
    Console.WriteLine("String");
}

*Okay, so technically double (and every floating point number type) can't represent every integer perfectly, because of how they're implemented in binary, but that's a whole other topic and too advanced for now, if you ever want to know more, check out Is floating point math broken?

  •  Tags:  
  • c#
  • Related