gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:03 25 4
gpt4 key购买 nike

我正在尝试将字符串“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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com