How to change number decimal seperator in C#?

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


How to change number decimal seperator in C#?
added 10 years 9 months ago

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.

- How to POST and GET url with parameters in C#?
- Open a text file and read line by line in C#
- Convert.ToInt32() vs Int32.Parse() in C#
- How to C# Linq Group By by paramater/field names
- Simple Injector
- How to check memory consumption in c#
- How can I implement C/C++ "union" in C#?
- How to Open and Read From Excel File in C#
- Weak References
- Lazy Initialization
- Enable Nuget Package Restore
- OnixS FIX Engine
- Rapid Addition FIX API
- How to change DateTime format in C#
- How to change number decimal seperator in C#?
2
1