System.FormatException: input line was not in the correct format

    private void ReadUnitPrice()
    {
        Console.Write("Enter the unit gross price: ");
        unitPrice = double.Parse(Console.ReadLine());
    }

This should work, but I'm missing something obvious. Whenever I type double, it gives me an error: System.FormatException: the input line was not in the correct format. Note that 'unitPrice' is declared as double.

+5
source share
1 answer

, , . Double.TryParse(), , .

public static bool TryParse(
    string s,
    NumberStyles style,
    IFormatProvider provider,
    out double result
)

TryParse Parse (String, NumberStyles, IFormatProvider), , , . , , . , .

EDIT:

if(!double.TryParse(Console.ReadLine(), out unitPrice))
{
    // parse error
}else
{
   // all is ok, unitPrice contains valid double value
}

:

double.TryParse(Console.ReadLine(), 
                NumberStyle.Float, 
                CultureInfo.CurrentCulture, 
                out unitPrice))
+6

All Articles