C# 德国 UI 文化 de-DE 十进制更改为 asp.net 中的逗号值问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11260111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:09:26  来源:igfitidea点击:

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

c#asp.net

提问by Kumar Gaurav

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

我在我的 asp.net 应用程序中使用德国 UI 文化。我正在根据下拉列表中选择的语言更改我的应用程序的 UI 文化,在下拉选择的索引更改中,我正在使用此代码

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.

我的问题是在将语言更改为 de-DE 后,我的应用程序中的所有十进制值都被更改为逗号,所有像 5.12 这样的十进制数都变成了 5,12,所有十进制值都更改为逗号。如何获得没有逗号的十进制值。

采纳答案by Hitesh Patel

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

它给出了结果 d = 50.3

回答by swapneel

In german culture Decimal is denoated by ","

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

在德国文化中,十进制表示为“,”

德国文化使用“.”。作为组分隔符和“,”作为小数点分隔符

Refer wiki link

参考维基链接

回答by Adriano Repetti

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

Thread.CurrentUICulture旨在用于用户界面,它是用于显示文本、方向等的语言。
Thread.CurrentCulture旨在用于解析/格式化东西。日期和时间、数字和字符串比较(例如)。

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.

如果您只想更改 UI 语言(并保持其他所有内容与您的 Web 服务器的文化一致),您只需修改Thread.CurrentUICulture.

回答by Ben

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

这种行为对德语来说是正确的。但是,您可能想要使用 . 在序列化为 XML、插入数据库等时,您可以使用不变区域性进行格式化。您可以在以下链接中了解有关不变文化的更多信息: CultureInfo.InvariantCulture 是什么意思