十进制铸造 [英] Decimal Casting
问题描述
我有一个十进制数是这样的:
I have a decimal number like this:
62.000,0000000
62.000,0000000
我要投的十进制转换成int;它总是具有零十进制数;所以我不会丢失任何精度。我想是这样的:
I need to cast that decimal into int; it will always have zero decimal numbers; so I wont loose any precision. What I want is this:
62.000
存储在C#int变量
我尝试了很多方法,但它总是给我一个错误,该字符串是不正确的格式,这是我tryied的方式之一:
I try many ways but it always give me an error, that the string isn't in the correct format, this is one of the ways i tryied:
int.Parse("62.000,0000000");
希望你能帮助我,谢谢。
Hope you can help me, thanks.
推荐答案
您需要分析它在正确的文化。例如,德国的文化用。作为一个组分隔符和,作为小数点分隔符 - 所以此代码的工作:
You need to parse it in the right culture. For example, the German culture uses "." as a group separator and "," as the decimal separator - so this code works:
CultureInfo german = new CultureInfo("de-DE");
decimal d = decimal.Parse("62.000,0000000", german);
int i = (int) d;
Console.WriteLine(i); // Prints 62000
是你后?需要注意的是,如果你在一个不同的文化格式化...
Is that what you're after? Note that this will then fail if you present it with a number formatted in a different culture...
编辑:里德在他的评论中提到的,如果你的真正的文化是西班牙,然后交换去-DE的ES-ES的好办法。
As Reed mentions in his comment, if your real culture is Spain, then exchange "de-DE" for "es-ES" for good measure.
这篇关于十进制铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!