0

我不明白为什么它会这样

a = "1,00"
IFormatProvider numberlanguagestyle = CultureInfo.CreateSpecificCulture("en-US");
pricetopay = (int)decimal.Parse(a, numberlanguagestyle);
Console.writeline(pricetopay)

这输出100

尽管

a = "1"
IFormatProvider numberlanguagestyle = CultureInfo.CreateSpecificCulture("en-US");
pricetopay = (int)decimal.Parse(a, numberlanguagestyle);
Console.writeline(pricetopay)

这输出1

现在这在我的 vs2010 版本中运行良好,但是我们有不同的程序员使用不同的语言,我们讨论了舍入错误和类似的东西虽然上面的代码在我们的应用程序中工作正常,但我想知道它为什么会这样。

4

3 回答 3

4

那是因为,用作数字组分隔符,而不是用作小数点。通常你会用它来分组数千,比如1,000,000一百万。在en-US文化中,您需要使用.小数点。

于 2013-11-14T14:46:35.653 回答
3

en-US文化中,逗号是千位分隔符,因此基本上被忽略。"1,00"因此被解释为"100",也就是 100。

于 2013-11-14T14:47:17.897 回答
2

我可能误解了你的问题(因为我看不出它与舍入有什么关系),但在美国,小数点分隔符是".",不是",",所以解析器(正确地)解释1,00为 100。

于 2013-11-14T14:48:16.367 回答