93

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?

1
  • Use: Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); and all xyz.ToString() in this thread will use your desired format. Commented Jan 28, 2020 at 18:39

7 Answers 7

204

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);
Sign up to request clarification or add additional context in comments.

2 Comments

I like this method more because its language independent!
This didn't work for me. Seems the CultureInfo always takes precedence. There was a similar issue here: stackoverflow.com/questions/43850022/… where the NFI didn't work as expected mentioning that the call should be like this: ToString("N", nfi) But this didn't work for me either. It respected the decimal digits but not the separator
43

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"));
    }
}

3 Comments

Maybe someone downvoted because, while this works like a charm, it could be done in a prettier way, by setting the cultureinfo globally. Btw. i'm not the downvoter. :)
@Thomas: Indeed, although when the OP says "I need it quite often in my program", I interpreted this to mean something like "often but not every time".
@ThomasTeilmann Maybe someone downvoted because they're illiterate. This is a clear, simple, and readable answer.
33

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");

2 Comments

Well, I don't need it in the whole program I need it to fit the format of PathGeometry in WPF where the decimal points are expected. But it turns out that this solution doesn't hurt anything else. Thank you!
@womp By far better answer! Does this also apply to sub threads if I set that to my main thread ? I would like to apply this setting for the whole execution time of my assembly.
19

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.)

3 Comments

If you try to do that, you get an "Instance is read-only" error.
@MariusStanescu I think this only happens when using a nuetral culture as adrianbanks noted at the end of his answer.
@patrickbadley I've just tried this solution and encountered the "read-only" error. Adding "Culture.IsNeutralCulture" in "Watch window" cleary showed that the culture wasn't neutral. However, I also noticed that it was readonly. Cloning the culture seem to be the right way around this. See the following answer.
15
Convert.ToString(value, CultureInfo.InvariantCulture);

Comments

3

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>

Comments

2

A simpler but less ellegant solution:

string str = number.toString("0.00000").Replace(",",".");

Is short and works, but you must specify your current separator in the first argument of the "Replace" function. This is not a good idea to run this solution on software that can run in different languages systems, is not that flexible.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.