c# - Convert.ToDecimal 为简单字符串抛出异常

标签 c# string

我正在尝试将字符串“10.00”转换为十进制,如下所示:

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

但由于某些令人难以置信的原因,我得到了这个错误:

Input string was not in a correct format. at 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 Presentation.Models.OrderBaseModel.Response(FormCollection response, String responseUrl)

还有更深层次的原因吗? “10.00”字符串怎么会无法解析??

最佳答案

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

为确保 10.00 能够解析,您需要使用能够解析它的区域性。

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

关于c# - Convert.ToDecimal 为简单字符串抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28490568/

相关文章:

c# - c++ dll结构到c#

c# - WPF ICommand CanExecute():RaiseCanExecuteChanged()还是通过DispatchTimer自动处理?

string - VB6: "string = string * integer"是做什么的?

python - 仅从字符串中删除最后一次出现的单词

python - 如何在 Python 中使用空分隔符拆分字符串

c# - 使用 Xamarin 和 C# 的 ARKit body 跟踪不准确

c# - 如何定义相互依赖的属性

c# - 在 Windows 8 中向 groupeditemspage 添加内容

java - 与 JOptionPane 相关的字符串连接

C 拆分字符串