简体   繁体   English

CultureInfo.CurrentCulture不包含正确的值->数字格式问题

[英]CultureInfo.CurrentCulture does not contain the correct value --> numberformat problems

I need to write doubles to a csv file in a wpf mvvm application. 我需要在wpf mvvm应用程序中将双打写入csv文件。 These doubles are originally strings set in invariant culture. 这些双打最初是在不变文化中设置的字符串。 I recieve them like so from the server side application and parse them with 我像这样从服务器端应用程序接收它们,并使用

bool result = double.TryParse(row[0], NumberStyles.Any, CultureInfo.InvariantCulture, out value);  

However when I try to write them to the csv file (either with "" + value , value.ToString() , value.ToString(CultureInfo.CurrentCulture) or value.ToString(System.Globalization.CultureInfo.CurrentCulture) ), the current culture seems to be EN-US while it should be NL (dutch). 但是,当我尝试将它们写入csv文件(使用"" + valuevalue.ToString()value.ToString(CultureInfo.CurrentCulture)value.ToString(System.Globalization.CultureInfo.CurrentCulture) )时,当前文化似乎是EN-US,而应该是NL(荷兰语)。 My browser settings, windows settings, keyboard settings... are all set to dutch, so when I check my regional number settings it contains the decimal separator ",", but my application separates the decimals with "." 我的浏览器设置,Windows设置,键盘设置...都设置为荷兰语,因此当我检查区域号设置时,它包含小数点分隔符“,”,但是我的应用程序用“。”分隔小数点。 since the culture is EN-US. 因为文化是EN-US。

So when I write my csv file and open it in excel it contains decimal values separated with "." 因此,当我编写csv文件并在excel中打开它时,它包含用“。”分隔的十进制值。 instead of ",". 代替 ”,”。 This way my excel does not see these values as decimal values since they are not in the excel number culture/my windows settings culture. 这样,我的excel不会将这些值视为十进制值,因为它们不在excel数字区域性/我的Windows设置区域性中。

Does anyone know what's going on? 有人知道发生了什么吗?

Edit 编辑

Just to be clear. 只是要清楚。 I'm not looking for a way to set the culture hard coded. 我不是在寻找一种对文化进行硬编码的方法。 I specifically need the culture set in windows, the one which excel uses. 我特别需要在Excel中使用的Windows中设置的区域性。

For further reference here's what the problem was: 作为进一步参考,这里是问题所在:

We have a pretty large application where it is possible to override the culture for datetime display etc... Once you set the current thread culture to a new culture, the original one cannot be accessed anymore. 我们有一个非常大的应用程序,可以覆盖日期时间显示等区域性。一旦将当前线程区域性设置为新区域性,就无法再访问原始区域性。

So to be able to access the windows culture I created a property which remembers the currentculture before it was changed to the user setting of the application and used that one to export my data to a csv file. 为了能够访问Windows区域性,我创建了一个属性,该属性可以在将当前区域性更改为应用程序的用户设置之前记住当前区域性,并使用该区域性将我的数据导出到csv文件。

Have you override base.InitializeCulture() ; 您是否覆盖了base.InitializeCulture() ;

below sample code is just for understanding..may be you get some idea 下面的示例代码仅用于理解..也许您有所了解

    if (Session["CurrentUI"] != null)
    {
        String selectedLanguage = (string)Session["CurrentUI"];
        UICulture = selectedLanguage;
        Culture = selectedLanguage;

        Thread.CurrentThread.CurrentCulture =
            CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new
            CultureInfo(selectedLanguage);
    }

    base.InitializeCulture();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM