7

I am using german UI Culture in my asp.net application. I am changing my application's UI culture based on the language selected in the dropdown, on dropdown selected index change i am using this code

Thread.CurrentThread.CurrentCulture = new CultureInfo(this.lstLanguage.SelectedValue);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.lstLanguage.SelectedValue);

the Dropdown is as below

<asp:DropDownList cssClass="ddllanguage" ValidationGroup="b" runat="server" ID="lstLanguage" AutoPostBack="True"  OnSelectedIndexChanged="LstLanguage_SelectedIndexChanged" meta:resourcekey="lstLanguage">                    
  <asp:ListItem Value="en-US" Text="English" meta:resourcekey="ListItemResource2" ></asp:ListItem>
  <asp:ListItem Value="de-DE" Text="Deutsch" meta:resourcekey="ListItemResource3"></asp:ListItem>
</asp:DropDownList>

My problem is after changing the language to de-DE all the decimal values in my application are being changed as comma, all the decimal number like 5.12 is coming as 5,12 all the decimal values are changed to comma. How to get decimal values as it is without comma.

Kumar Gaurav
  • 899
  • 5
  • 19
  • 35
  • 1
    You shouldn't do this. "My problem is after changing the language to de-DE all the decimal values in my application are being changed as comma, [...]". It may seem as a problem to you, but this is what a german user would expect after changing the language from english to german. – M.Stramm Jan 18 '18 at 12:45

4 Answers4

8

Thread.CurrentUICulture is intended to be used for User Interface, it's the language used to display text, orientation and so on.
Thread.CurrentCulture is intended to be used for parsing/formatting stuffs. Date and time, numbers and string comparison (for example).

If you want to change only UI language (and to keep everything else with the culture of your web server) you have to modify only Thread.CurrentUICulture.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
8

you can change number format info as per below code

Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

try below code for parsing the decimal values as per culture.

string str = "50,3";
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
double d = double.Parse(str, Thread.CurrentThread.CurrentCulture.NumberFormat);

it gives result d = 50.3

Hitesh Patel
  • 439
  • 2
  • 10
  • 3
    @Gaurav it's the worst thing you can do for your customers. If they see text in German they may expect to enter data (number, dates, strings for comparison and ordering) in German. If they know to enter data using a "neutral" culture (en-US) then they'll go crazy because you mix different settings (dates in German, decimals in English, thousands won't work and ordering in German again). – Adriano Repetti Jun 29 '12 at 10:41
  • 2
    @Gaurav, on second thought I agree with Adriano here: although it is possible to tweak the culture information, it won't help your users realize they have to use the `.` decimal separator instead of `,`. Depending on the way you perform your computations, methods like `Double.Parse()` can use the German culture and recognize `,` as the decimal separator. – Frédéric Hamidi Jun 29 '12 at 10:44
  • @Gaurav I have edited my answer please see the changes and mark as answer if it hepls you – Hitesh Patel Jun 29 '12 at 10:57
  • @Adriano, Hamidi is it ok now? – Hitesh Patel Jun 29 '12 at 10:58
  • @HiteshPatel No, it isn't. If you set CurrentCulture you do not need to specify an IFormatProvider for Parse (see my answer) if they're the same. If you specify an IFormatProvider then CurrentCulture is ignored by Parse (see link posted by rajnikant to Jon's answer). – Adriano Repetti Jun 29 '12 at 11:02
4

In german culture Decimal is denoated by ","

German culture uses "." as a group separator and "," as the decimal separator

Refer wiki link

swapneel
  • 3,061
  • 1
  • 25
  • 32
  • its Ok but getting changed all the decimal with comma, i am unable to process them or do calculation with them, how to parse these values back to decimal. – Kumar Gaurav Jun 29 '12 at 10:32
  • Change values to en-us while doing calculation. see jon's answer – swapneel Jun 29 '12 at 10:36
1

This behavior is correct for German. However, you might want to use . When serializing to XML, inserting to database, etc. for doing so you can format using invariant culture. You can learn more about Invariant Culture in the following link: What does CultureInfo.InvariantCulture mean

Community
  • 1
  • 1
Ben
  • 398
  • 2
  • 8