87

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?

sharptooth
  • 167,383
  • 100
  • 513
  • 979
MartyIX
  • 27,828
  • 29
  • 136
  • 207
  • Use: Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); and all xyz.ToString() in this thread will use your desired format. – Elmue Jan 28 '20 at 18:39

7 Answers7

189

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);
Peter VARGA
  • 4,780
  • 3
  • 39
  • 75
romanz
  • 1,936
  • 1
  • 12
  • 3
  • 8
    I like this method more because its language independent! – AleX_ Jul 05 '16 at 14:04
  • This didn't work for me. Seems the CultureInfo always takes precedence. There was a similar issue here: https://stackoverflow.com/questions/43850022/numberformatinfo-numberdecimaldigits-not-working 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 – F. ALA Jun 15 '23 at 13:57
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"));
    }
}
Marc
  • 6,749
  • 9
  • 47
  • 78
LukeH
  • 263,068
  • 57
  • 365
  • 409
  • 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. :) – Teilmann Sep 21 '15 at 07:03
  • 2
    @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"*. – LukeH Sep 21 '15 at 09:33
  • 1
    @ThomasTeilmann Maybe someone downvoted because they're illiterate. This is a clear, simple, and readable answer. – ProfK Oct 28 '19 at 11:18
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");
womp
  • 115,835
  • 26
  • 236
  • 269
  • 1
    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! – MartyIX Jun 28 '10 at 19:49
  • @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. – Bitterblue Mar 14 '13 at 09:54
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.)

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • 10
    If you try to do that, you get an "Instance is read-only" error. – Marius Stănescu Sep 09 '15 at 08:31
  • @MariusStanescu I think this only happens when using a nuetral culture as adrianbanks noted at the end of his answer. – patrickbadley Feb 11 '16 at 19:28
  • 2
    @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](http://stackoverflow.com/a/3912937/4625305). – AXMIM Aug 05 '16 at 14:34
11
Convert.ToString(value, CultureInfo.InvariantCulture);
toree
  • 399
  • 2
  • 8
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>
Frederik Struck-Schøning
  • 12,981
  • 8
  • 59
  • 68
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
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.