tags:

views:

126

answers:

3
+2  Q: 

Decimal Casting

Hi, I have a decimal number like this:

62.000,0000000

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

Stored on an int variable in c#.

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.

+2  A: 

Why don't you just cast it to int instead of parsing?

Giorgi
His example works with a string. So who knows where he gets that number from and if he can change the delivery of the decimal to not show the dots and commas and omit the unnecessary precision?
Peter Schuetze
+12  A: 

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...

EDIT: As Reed mentions in his comment, if your real culture is Spain, then exchange "de-DE" for "es-ES" for good measure.

Jon Skeet
Perfect answer - just FYI - I'm pretty sure (from one of this other posts), that lidermin's from spain, so using "es-ES" should give him the proper culture.
Reed Copsey
+7  A: 

You need to specify your culture correctly:

var ci = CultureInfo.CreateSpecificCulture("es-ES");
int value = (int) decimal.Parse("60.000,000000", ci);

If you do this, it will correctly convert the number.

Reed Copsey