German UI Culture de-DE decimal changing to comma value issue in asp.net
Asked Answered
R

4

9

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.

Rumal answered 29/6, 2012 at 10:25 Comment(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.Keelin
P
9

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

Pyroligneous answered 29/6, 2012 at 10:32 Comment(5)
@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).Vermiculate
@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.Phyto
@Gaurav I have edited my answer please see the changes and mark as answer if it hepls youPyroligneous
@Adriano, Hamidi is it ok now?Pyroligneous
@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).Vermiculate
V
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.

Vermiculate answered 29/6, 2012 at 10:29 Comment(0)
C
4

In german culture Decimal is denoated by ","

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

Refer wiki link

Calvities answered 29/6, 2012 at 10:29 Comment(2)
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.Rumal
Change values to en-us while doing calculation. see jon's answerCalvities
B
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

Bonedry answered 29/6, 2012 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.