Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CultureInfo.InvariantCulture default?

Tags:

c#

double

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.

like image 440
nhtrnm Avatar asked Oct 04 '12 14:10

nhtrnm


People also ask

How do you set CultureInfo InvariantCulture?

You specify the invariant culture by name by using an empty string ("") in the call to a CultureInfo instantiation method. CultureInfo. InvariantCulture also retrieves an instance of the invariant culture. It can be used in almost any method in the System.

What is CultureInfo InvariantCulture?

The CultureInfo. InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings. The default value is CultureInfo. InstalledUICulture so the default CultureInfo is depending on the executing OS's settings.

How do I change the current culture in C#?

CurrentCulture = New CultureInfo("th-TH", False) Console. WriteLine("CurrentCulture is now {0}.", CultureInfo.CurrentCulture.Name) ' Display the name of the current UI culture. Console. WriteLine("CurrentUICulture is {0}.", CultureInfo.CurrentUICulture.Name) ' Change the current UI culture to ja-JP.

What is CultureInfo C#?

CultureInfo provides information about a specific culture. The information includes the names for the culture, the writing system, the calendar used, the sort order of strings, and formatting for dates and numbers.


1 Answers

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.

like image 74
Guffa Avatar answered Sep 20 '22 17:09

Guffa