40

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.

nhtrnm
  • 1,411
  • 3
  • 15
  • 24
  • What will happen when you deploy your application in a different culture? – Jodrell Oct 04 '12 at 14:50
  • 1
    @Jodrell, it's a task on the server and my program will end with a Wrong Answer (WA) – nhtrnm Oct 04 '12 at 14:54
  • You should state why you 'need' a dot. Sounds like you want to write to some export filo or so. See my answer below. – Designpattern Jul 27 '13 at 04:31
  • Possible duplicate of [how to set default culture info for entire c# application](https://stackoverflow.com/questions/13354211/how-to-set-default-culture-info-for-entire-c-sharp-application) – Martin Valgur Sep 08 '18 at 16:45
  • 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. – MGOwen Sep 10 '19 at 04:15

7 Answers7

59

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 3
    Where exactly should write this code in my project? – Rapunzo Apr 21 '14 at 12:25
  • 4
    @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. – Guffa Apr 21 '14 at 12:32
  • 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. – Rapunzo Apr 21 '14 at 12:57
  • @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. – Guffa Apr 21 '14 at 15:55
  • But what is the default for new threads? Do they inherit it from the invoking thread or get the default again or? – Jan Hudec Mar 18 '16 at 12:37
  • 3
    @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: http://blog.rastating.com/setting-default-currentculture-in-all-versions-of-net/ – Guffa Mar 18 '16 at 18:50
  • 1
    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) – Gianluca Ghettini Apr 04 '16 at 12:59
  • @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. – Guffa Apr 04 '16 at 14:28
33

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;
Martin Valgur
  • 5,793
  • 1
  • 33
  • 45
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

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • Note, this works only for IIS. If you use Kestrel or want solution that works with both, better use other answers – dimaaan Aug 17 '23 at 11:18
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

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
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".

Oliver
  • 43,366
  • 8
  • 94
  • 151
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.

Designpattern
  • 184
  • 10
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.

Joe
  • 122,218
  • 32
  • 205
  • 338