read user input of double type

后端 未结 4 1312
無奈伤痛
無奈伤痛 2021-01-18 07:18

I have found this answered in other places using loops, but I wasn\'t sure if there is actually a function that I\'m not finding that makes this easier, or if this is a poss

相关标签:
4条回答
  • 2021-01-18 08:02

    You'll have to check the entire thing on it's way in.. as Console.Read() returns an integer.

    double totalSalary;
    if (!double.TryParse(Console.ReadLine(), out totalSalary)) {
        // .. error with input
    }
    // .. totalSalary is okay here.
    
    0 讨论(0)
  • 2021-01-18 08:02

    Try this:

    double Salary = Convert.ToDouble(Console.ReadLine());
    
    0 讨论(0)
  • 2021-01-18 08:12
    string input = Console.ReadLine();
    double d;
    if (!Double.TryParse(input, out d))
        Console.WriteLine("Wrong input");
    double r = d * Math.Pi;
    Console.WriteLine(r);
    
    0 讨论(0)
  • 2021-01-18 08:17

    Simplest answer to your question:

    double d = Double.Parse(Console.Readline());
    
    0 讨论(0)
提交回复
热议问题