Convert.ToDecimal抛出简单字符串的异常

时间:2015-02-13 00:34:34

标签: c# string

我正在尝试转换字符串" 10.00"像这样的小数:

decimal a = Convert.ToDecimal("10.00");

但出于某种令人难以置信的原因,我收到了这个错误:

  

输入字符串的格式不正确。 System.Number.StringToNumber(String str,NumberStyles options,NumberBuffer& number,NumberFormatInfo info,Boolean parseDecimal)at System.Number.ParseDecimal(String value,NumberStyles options,NumberFormatInfo numfmt)at System.Convert.ToDecimal(String value)at at Presentation.Models.OrderBaseModel.Response(FormCollection response,String responseUrl)

有更深层次的原因吗?怎么能" 10.00"字符串是不可解决的?

1 个答案:

答案 0 :(得分:4)

Convert.ToDecimal()调用Decimal.Parse()

为确保10.00将解析您需要使用将解析它的文化。

var culture = CultureInfo.InvariantCulture
decimal a = Decimal.Parse("10.00",culture);
相关问题