How to change number decimal seperator in C#?
Changing the decimal seperator in programming environments might be a very critical task in multi-language supporting applications. Each country has their own decimal seperator and you have to handle all the conversion tricks.
There are several ways to handle decimal seperator in C# programming language.
Creating Extension Methods to change number decimal seperator in C#
First method to change number decimal seperator in C# is to create an extension method that does the conversion for you as given below.
First, define your extension method, and then use it wherever you want.
public static class DoubleExtensions
{
public static string ToGBString(this double value)
{
return value.ToString(CultureInfo.GetCultureInfo("en-GB"));
}
}
// ...
Console.WriteLine(value.ToGBString());
Changing current culture of the application to manage number decimal seperator
Second method to change number decimal seperator in C# is editing the current culture:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");
Just change the decimal seperator:
Thread.CurrentCulture.NumberFormat. NumberDecimalSeparator = ".";
Editing number decimal seperator in C# through config file
Edit config file of your application:
<system.web>
...
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8"
culture="en-GB"
uiCulture="en-GB"
enableClientBasedCulture="false" />
...
<system.web>
NumberFormatInfo for changing number decimal seperator in CSharp
Use NumberFormatInfo.NumberDecimalSeparator Property to change seperator:
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
// Displays a value with the default separator (".").
Int64 myInt = 123456789;
Console.WriteLine( myInt.ToString( "N", nfi ) ); // 123,456,789.00
// Displays the same value with a blank as the separator.
nfi.NumberDecimalSeparator = " ";
Console.WriteLine( myInt.ToString( "N", nfi ) ); // 123,456,789 00
Contents related to 'How to change number decimal seperator in C#?'
Converting String to Double in C#: Several example codes that are parsing string to double in C# language.
How to change DateTime format in C#: Changing the format of DateTime in C# applications.