How to change symbol for decimal point in double.ToString()?
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");
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);
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.)
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"));
}
}