C# doubles 显示逗号​​而不是句号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/518625/
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-04 06:30:08  来源:igfitidea点击:

C# doubles show comma instead of period

c#currentculture

提问by Presidenten

I almost have the same problem as the guy in this thread:

我几乎和这个线程中的那个人有同样的问题:

Convert Float that has period instead of comma?

转换带有句点而不是逗号的浮点数?

So that my

这样我的

double x = 234.4;
string y = x.ToString();

I get y == "234,4";

我明白了y == "234,4"

Even worse ... Double.Parse("234.4")throws an exception.

更糟糕的是......Double.Parse("234.4")抛出异常。

I have written alot of code before I was asked to use period instead of comma, so I would prefer to have some way to change my CultureInfo at a global level.

在我被要求使用句点而不是逗号之前,我已经编写了很多代码,所以我更喜欢有某种方式在全局级别更改我的 CultureInfo

Is there some setting in the projects that I can do?

项目中有一些我可以做的设置吗?

I've tried:

我试过了:

        Application.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

They kind of work. They work for most part of the application, but all controls that reside in the tabs of my TabControl still wants to use my computers Localized CultureInfo.

他们有点工作。它们适用于应用程序的大部分内容,但驻留在我的 TabControl 选项卡中的所有控件仍然希望使用我的计算机 Localized CultureInfo

Any Ideas on how to solve this?

关于如何解决这个问题的任何想法?

采纳答案by Presidenten

Thanks to Florin Sabaus comment I found the solution, which was to place

感谢 Florin Sabaus 的评论,我找到了解决方案,即放置

        Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");

in Main() right before I created my form.

在我创建表单之前的 Main() 中。

Now I won't have to change all my .ToString() and Double.Parse() :-) Yey!

现在我不必更改我所有的 .ToString() 和 Double.Parse() :-) 是的!

回答by Florin Sabau

You could try to use

你可以尝试使用

double.Parse("...", CultureInfo.InvariantCulture)

and

x.ToString(CultureInfo.InvariantCulture)

in the parts of the program that you are positive you need to have decimal period instead of comma or other regional settings dependent decimal separator.

在您肯定的程序部分中,您需要使用小数点而不是逗号或其他区域设置相关的小数点分隔符。

Hope it helps.

希望能帮助到你。