可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
When I have such piece of code in C#:
double a = 0.003;
Console.WriteLine(a);
It prints "0,003".
If I have another piece of code:
double a = 0.003;
Console.WriteLine(a.ToString(CultureInfo.InvariantCulture));
It prints "0.003".
My problem is that I need a dot as decimal mark but C# makes a comma as default. Besides I don't want to type such a long line of code just for printing out a double variable.
回答1:
You can set the culture of the current thread to any culture you want:
Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
Note that changing the culture also affects things like string comparison and sorting, date formats and parsing of dates and numbers.
回答2:
1 Empty string specifies InvariantCulture in config.file
By default, Culture and UICulture are set to "" in the config.
<system.web>
<globalization culture="" />
</system.web>
2 You can also define on your Thread
回答3:
C# doesn't make it a comma by default, it's your culture. Try setting the culture explictly,
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
Link: http://msdn.microsoft.com/en-us/library/ms425914(v=office.12).aspx
回答4:
Since .NET Framework version 4.5 (and .NET Core/Standard 1.0) you can change the culture for the whole application domain, rather than just the current Thread, by modifying the CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture properties:
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
回答5:
If you never want culture-specific formatting of numbers and dates, you can set the culture once, perhaps at application startup .
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture
If it's an ASP.NET application, a simpler alternative is to set the culture in the <globalization>
configuration element of web.config.
Otherwise you don't have much alternative to specifying the culture explicitly. If you find yourself repetitively typing the same long line of code, do what you always do in this case: wrap it in a method.
回答6:
When you call WriteLine()
and give in a double it makes internally more or less this call:
Console.WriteLine(a.ToString(CultureInfo.CurrentCulture));
The task would be now to replace the CurrentCulture
with InvariantCulture
. This can be done by the following line of code:
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Now you'r thread is set the the InvariantCulture and your first call should also print "0.003".
回答7:
You can set CultureInfo.InvariantCulture
as default as shown above by @Guffa and the others.
But you have to have a clear idea why you do this. It will be ok if you do data export/import operations, but probably you wouldn't use it for strings presented to the user.
The Microsoft documentation states:
The invariant culture is culture-insensitive; it is associated with
the English language but not with any country/region. You specify the
invariant culture by name by using an empty string ("") in the call to
a CultureInfo instantiation method. ...
Unlike culture-sensitive data,
which is subject to change by user customization or by updates to the
.NET Framework or the operating system, invariant culture data is
stable over time and across installed cultures and cannot be
customized by users. This makes the invariant culture particularly
useful for operations that require culture-independent results, such
as formatting and parsing operations that persist formatted data, or
sorting and ordering operations that require that data be displayed in
a fixed order regardless of culture.