How to set CultureInfo.InvariantCulture default?
Asked Answered
R

8

43

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.

Remit answered 4/10, 2012 at 14:46 Comment(5)
What will happen when you deploy your application in a different culture?Hugmetight
@Jodrell, it's a task on the server and my program will end with a Wrong Answer (WA)Remit
You should state why you 'need' a dot. Sounds like you want to write to some export filo or so. See my answer below.Autocorrelation
Possible duplicate of how to set default culture info for entire c# applicationHedgerow
C# is not "making a comma as a default". The environment or computer you are running the code on has it's culture set to a place/language that uses commas for decimal places. Setting InvariantCulture works because you are telling it to format the number using the Invariant culture (which is like a default culture). Changing the culture in the code might not be the right answer at all, it depends on who this software is for.Propagandism
E
61

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.

Efren answered 4/10, 2012 at 14:50 Comment(8)
Where exactly should write this code in my project?Hysteresis
@Rapunzo: You can put it anywhere it will be run for each request, for example in the Application_BeginRequest in global.asax, or in the first event in your page classes.Efren
Thank you as you said I put it to MDI form in my desktop project and numericUpDown's are working fine, but now datetimes giving me error.. can you give me a clue about it also if you dont mind.Hysteresis
@Rapunzo: You can create a custom culture info object where you specify the date settings and number settings that you need, if you can't find a region specific culture that fits your needs.Efren
But what is the default for new threads? Do they inherit it from the invoking thread or get the default again or?Historian
@JanHudec: The culture is not inherited when you start a new thread, there is a static member in the Thread class that determines the culture for new threads. More reading: blog.rastating.com/…Efren
uhm... I don't think this is a good solution. In a single thread app it could be fine but on a multi thread env this is not going to work (well, it will work just for the threads that execute such line)Hovis
@GianlucaGhettini: The point of specifying the culture for the thread is that it won't affect other threads. There is also an approach to set the culture for the entire application (see Aghilas Yakoubs answer), so there are three levels where you can specify the culture; globally, for the thread, or in each call. For some applications it works to set the culture globally, but other applications will need the other levels.Efren
H
37

Since .NET Framework version 4.5 and .NET Core/Standard 1.0 you can change the culture for the whole application, rather than just the current Thread, by modifying the CultureInfo.DefaultThreadCurrentCulture and CultureInfo.DefaultThreadCurrentUICulture properties:

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;
Hedgerow answered 8/9, 2018 at 16:44 Comment(0)
R
10

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

Roseannaroseanne answered 4/10, 2012 at 14:53 Comment(1)
Note, this works only for IIS. If you use Kestrel or want solution that works with both, better use other answersCocteau
T
6

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

Tyratyrannical answered 4/10, 2012 at 14:51 Comment(0)
A
3

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".

Amling answered 4/10, 2012 at 14:53 Comment(0)
A
3

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.

Autocorrelation answered 27/7, 2013 at 4:38 Comment(0)
T
2

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.

Truong answered 4/10, 2012 at 14:53 Comment(1)
setting the culture using thread.currentThread.currentculture will only affect the current threadHovis
S
2

Starting with .Net Core 2.0, you can set a whole project to Globalization Invariant Mode, which completely removes any localization support. This will remove localization dependencies and thereby reduce distribution size.

You can enable Invariant mode in your csproj, the runtime config, or with an environment variable.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <InvariantGlobalization>true</InvariantGlobalization>
  </PropertyGroup>

</Project>
Siddons answered 11/4 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.