简体   繁体   中英

I can't convert to double in C# using visual studio 2019

I need help with using Convert.ToDouble. I am struggling to convert to double in C# and visual studio gives an exception. Do I need to install extensions?

this is all the code

using System;

namespace VariablesAndDataTypes
{
    class Program
    {
        static void Main(string[] args)
        {
            //Variables dclarations amd types
            string name;
            int age;
            double salary;
            char gender;
            bool working;

            //Prompt users for Input
            Console.Write("Enter your name: ");
            name = Console.ReadLine();

            Console.Write("Enter your age: ");
            age = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter your salary: ");
            salary = Convert.ToDouble(Console.ReadLine()); // the exception is thrown here
            Console.Write("Enter your gender (M/F): ");
            gender = Convert.ToChar(Console.ReadLine());

            Console.Write("Are you working (true/false): ");
            working =Convert.ToBoolean(Console.ReadLine());

            //Print to Screen
            Console.WriteLine("Your name is: " + name);
            Console.WriteLine("Your age is: {0}", age);
            Console.WriteLine($"Your salary is: {0}" + salary); // when  I write a double it gives and error and the console stops responding.
            Console.WriteLine("Your gender is: " + gender);
            Console.WriteLine("You are Employed: " + working);
        }
    }
}

PLEASE NOTE: I am new to programming

Welcome to C# @Mickey

It could be that you've used the string interpolation($) wrongly

Try this

Console.WriteLine($"Your salary is: {salary}");

It would be quite helpful if you shared the exception you got though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM