Có thể bạn đang sử dụng biểu tượng tách dấu phẩy sai hoặc thậm chí tạo ra lỗi khác khi chỉ định giá trị kép. Dù sao trong những trường hợp như vậy, bạn phải sử dụng phương pháp Double.TryParse() an toàn ngoại trừ và cho phép chỉ định nhà cung cấp định dạng, về cơ bản văn hóa sẽ được sử dụng.
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: trả lời bình luận
if(!double.TryParse(Console.ReadLine(), out unitPrice))
{
// parse error
}else
{
// all is ok, unitPrice contains valid double value
}
Ngoài ra bạn có thể thử:
double.TryParse(Console.ReadLine(),
NumberStyle.Float,
CultureInfo.CurrentCulture,
out unitPrice))
gì giá trị mà bạn đang nhập? –
giá trị trong vòng 0-10 như 4.5 hoặc 5.5 –