请遵守以下简单代码。为什么变量inVariant打印时不带小数点。我要小数点,怎么实现呢?

decimal actualVal = 1247315.93m;

string inSwedish = actualVal.ToString(CultureInfo.CreateSpecificCulture("sv-SE"));

decimal inVarient = decimal.Parse(inSwedish, CultureInfo.InvariantCulture);

Console.WriteLine(inSwedish);  //prints 1247315,93 (as intended)
Console.WriteLine(inVarient);  //prints 124731593 (I need 1247315.93)
Console.Read();

最佳答案

这是因为您试图解析一个表示瑞典文化格式的十进制字符串,但您试图解析不将逗号作为小数点的不变文化。您需要这样的东西:

decimal actualVal = 1247315.93m;
var culture = CultureInfo.CreateSpecificCulture("sv-SE");

string inSwedish = actualVal.ToString(culture));

decimal invariant = decimal.Parse(inSwedish, culture);

Console.WriteLine(inSwedish);
Console.WriteLine(invariant.ToString(CultureInfo.InvariantCulture));
Console.Read();

关于c# - 不变的十进制变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22325972/

10-12 22:34