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?
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.
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 %.
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.
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);
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")); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With