将此字符串转换为十进制
问题描述:
听起来很简单,但是当我尝试实现这一目标时,我对格式化程序如何进行转换存有疑问,这是一些我需要转换为十进制的字符串示例
Sounds easy but when I tried to achieve i'm stock about how is the formatter to make this conversion this are some examples of strings that i need to convert to decimal
00.24
48.34
01.24
有人知道我该怎么做到吗?我这样尝试过
Does anybody know how can i Accomplish this?? I tried like this
try
{
decimal x = Convert.ToDecimal("00.24", );
//Which formatter do I need to pass??
decimal x = Convert.ToDecimal("00.24", Formatter???);
}
Catch(Exception e)
{
throw new Exception()
}
但是它不起作用,因为结果是24D,我需要0.24D
But It doesn't work because the result it's 24D and i need 0.24D
答
我怀疑您的系统语言不是英语,并且具有不同的数字格式设置规则。尝试将不变区域性作为格式提供者传递:
I suspect your system culture is not English and has different number formatting rules. Try passing the invariant culture as the format provider:
decimal d = Convert.ToDecimal("00.24", CultureInfo.InvariantCulture);
您还可以使用 Decimal.Parse
:
decimal d = Decimal.Parse("00.24", CultureInfo.InvariantCulture);