Może to oznaczać, że używasz niewłaściwego symbolu separacji przecinkami lub nawet popełniłeś inny błąd, określając podwójną wartość. W każdym razie w takich przypadkach należy użyć metody Double.TryParse(), która jest bezpieczna pod względem wyjątku i umożliwia określenie dostawcy formatu, w zasadzie w celu wykorzystania kultury.
public static bool TryParse(
string s,
NumberStyles style,
IFormatProvider provider,
out double result
)
The TryParse method is like the Parse(String, NumberStyles, IFormatProvider) method, except this method does not throw an exception if the conversion fails. If the conversion succeeds, the return value is true and the result parameter is set to the outcome of the conversion. If the conversion fails, the return value is false and the result parameter is set to zero.
EDIT: Odpowiedź na komentarz
if(!double.TryParse(Console.ReadLine(), out unitPrice))
{
// parse error
}else
{
// all is ok, unitPrice contains valid double value
}
Ponadto można spróbować:
double.TryParse(Console.ReadLine(),
NumberStyle.Float,
CultureInfo.CurrentCulture,
out unitPrice))
Jakie wartości są Państwo wprowadzania? –
wartości w zakresie 0-10 jak 4,5 lub 5,5 –