Use double.ToString("N1") :
double d1 = 1d; double d2 = 0.2423423d; double d3 = 0.1d; double d4 = 1234d; Console.WriteLine(d1.ToString("N1")); Console.WriteLine(d2.ToString("N1")); Console.WriteLine(d3.ToString("N1")); Console.WriteLine(d4.ToString("N1"));
Demo
Standard number format strings
Numeric ("N") format specifier
Update
(1.234) .ToString ("N1") creates 1.2 and in addition to removing extra decimal digits, it also adds a thousands separator
Well, maybe you need to implement a custom NumberFormatInfo object, which you can get from the current CultureInfo and use in double.ToString :
var culture = CultureInfo.CurrentCulture; var customNfi = (NumberFormatInfo)culture.NumberFormat.Clone(); customNfi.NumberDecimalDigits = 1; customNfi.NumberGroupSeparator = ""; Console.WriteLine(d1.ToString(customNfi));
Please note that you need to clone it, since it is the default by default.
Demo
Tim schmelter
source share