How to change symbol for decimal point in double.ToString()?

C#.NetDoubleTostring

C# Problem Overview


I would like to change decimal point to another character in C#. I have a double variable value

double value;

and when I use the command:

Console.WriteLine(value.ToString()); // output is 1,25

I know I can do this:

Console.WriteLine(value.ToString(
    CultureInfo.CreateSpecificCulture("en-GB"))); // output is 1.25

but I don't like it very much because it's very long and I need it quite often in my program.

Is there a shorter version for setting "decimal point" really as point and not comma as is in my culture is usual?

C# Solutions


Solution 1 - C#

Some shortcut is to create a NumberFormatInfo class, set its NumberDecimalSeparator property to "." and use the class as parameter to ToString() method whenever u need it.

using System.Globalization;

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ".";

value.ToString(nfi);

Solution 2 - C#

Create an extension method?

Console.WriteLine(value.ToGBString());

// ...

public static class DoubleExtensions
{
    public static string ToGBString(this double value)
    {
        return value.ToString(CultureInfo.GetCultureInfo("en-GB"));
    }
}

Solution 3 - C#

Perhaps I'm misunderstanding the intent of your question, so correct me if I'm wrong, but can't you apply the culture settings globally once, and then not worry about customizing every write statement?

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB");

Solution 4 - C#

You can change the decimal separator by changing the culture used to display the number. Beware however that this will change everything else about the number (eg. grouping separator, grouping sizes, number of decimal places). From your question, it looks like you are defaulting to a culture that uses a comma as a decimal separator.

To change just the decimal separator without changing the culture, you can modify the NumberDecimalSeparator property of the current culture's NumberFormatInfo.

Thread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";

This will modify the current culture of the thread. All output will now be altered, meaning that you can just use value.ToString() to output the format you want, without worrying about changing the culture each time you output a number.

(Note that a neutral culture cannot have its decimal separator changed.)

Solution 5 - C#

Convert.ToString(value, CultureInfo.InvariantCulture);

Solution 6 - C#

If you have an Asp.Net web application, you can also set it in the web.config so that it is the same throughout the whole web application

<system.web>
    ...
    <globalization 
        requestEncoding="utf-8" 
        responseEncoding="utf-8" 
        culture="en-GB" 
        uiCulture="en-GB" 
        enableClientBasedCulture="false"/>
    ...
</system.web>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartyIXView Question on Stackoverflow
Solution 1 - C#romanzView Answer on Stackoverflow
Solution 2 - C#LukeHView Answer on Stackoverflow
Solution 3 - C#wompView Answer on Stackoverflow
Solution 4 - C#adrianbanksView Answer on Stackoverflow
Solution 5 - C#toreeView Answer on Stackoverflow
Solution 6 - C#JP HellemonsView Answer on Stackoverflow