Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 338
Martin Vseticka Avatar asked Jun 28 '10 19:06

Martin Vseticka


People also ask

How do you convert decimal to ToString?

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal. ToString(String, IFormatProvider) method.

How do you format a string to show two decimal places?

String strDouble = String. format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do I change the decimal separator in SQL Server?

You can first replace thousand separator comma(,) to a Zero length string (''), and then you can replace Decimal('. ') to comma(',') in the same select statement.


2 Answers

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); 
like image 192
romanz Avatar answered Oct 03 '22 11:10

romanz


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"));     } } 
like image 30
LukeH Avatar answered Oct 03 '22 13:10

LukeH