I've looked around this forum, and the rest of the internet, but my code is not working. Here is the problem:

I read XML files that are in en-US culture. The decimal separator is a dot ".".

Given the string "123.45", I want to convert the separator from a dot, to a comma. The current code doesn't work.

String tmp = "123.45";
System.Globalization.CultureInfo myInfo = System.Globalization.CultureInfo.CurrentUICulture;
/****The above line returns my culture, es-ES where the number separator is ","***/
System.Windows.Forms.MessageBox.Show("New value: "+ Decimal.Parse(test, myInfo));  
/***The above line displays the result "12345"***/

I have a lot of variables to put through this process so parsing strings isn't really useful at this point.

Any help is appreciated.

7 Years
Discussion Span
Last Post by charlybones

Of course it doesn't work, you are parsing en-US as es-ES. You need to do the parse first, then convert cultures for output.

String tmep = "123.45";
Decimal value = Decimal.Parse(test, system.Globalization.CultureInfo.InvariantCulture);
System.Windows.Forms.MessageBox.Show("New value: " + value);

This should work, it's hard to test because my UI culture is en-US :)


Mine is nl-BE also with comma as decimal point.
The MessageBox gives 123,45 (with comma) if I execute the OPs code.
Perhaps try something like this?

Decimal d = Decimal.Parse(tmp, myInfo); //123.45
            string str = d.ToString(CultureInfo.GetCultureInfo("es-ES").NumberFormat);//123,45

Edited by ddanbe: n/a


Try this:

string a = "12.34";
decimal b = Convert.ToDecimal(a, System.Globalization.CultureInfo.InvariantCulture);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.