Question

This is probably dumb but it's giving me a hard time. I need to convert/format a double to string with a mandatory decimal point.

1         => 1.0
0.2423423 => 0.2423423
0.1       => 0.1
1234      => 1234.0

Basically, I want to output all decimals but also make sure the rounded values have the redundant .0 too. I am sure there is a simple way to achieve this.

Was it helpful?

Solution 2

There is not a built in method to append a mandatory .0 to the end of whole numbers with the .ToString() method, as the existing formats will truncate or round based on the number of decimal places you specify.

My suggestion is to just roll your own implementation with an extension method

public static String ToDecmialString(this double source)
{
    if ((source % 1) == 0)
        return source.ToString("f1");
    else
        return source.ToString();

}

And the usage:

double d1 = 1;
double d2 = 0.2423423;
double d3 = 0.1;
double d4 = 1234;
Console.WriteLine(d1.ToDecimalString());
Console.WriteLine(d2.ToDecimalString());
Console.WriteLine(d3.ToDecimalString());
Console.WriteLine(d4.ToDecimalString());

Results in this output:

1.0
0.2423423
0.1
1234.0

OTHER TIPS

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 Numeric Format Strings

The Numeric ("N") Format Specifier

Update

(1.234).ToString("N1") produces 1.2 and in addition to removing additional decimal digits, it also adds a thousands separator

Well, perhaps you need to implement a custom NumberFormatInfo object which you can derive 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));

Note that you need to clone it since it's readonly by default.

Demo

You could do something like this: if the number doesn't have decimal points you can format its output to enforce one decimal 0 and if it has decimal places, just use ToString();

double a1 = 1;
double a2 = 0.2423423;
string result = string.Empty;
if(a1 - Math.Floor(a1) >0.0)
       result = a1.ToString();    
else
       result = a1.ToString("F1"); 

if (a2 - Math.Floor(a2) > 0.0)
       result = a2.ToString();
else
       result = a2.ToString("F1");

When you use "F" as formatting, the output won't contain thousands separator and the number that follows it specifies the number of decimal places.

Double provides a method ToString() where you can pass an IFormatProvider-object stating how you want your double to be converted.

Additionally, it should display trailing 0 at all costs.

value = 16034.125E21;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console: 
//       -16325.62015 
//       -16325.62015 
//       -16325,62015 
//       1.6034125E+25 
//       1.6034125E+25 
//       1,6034125E+25

Here is the documentation from MSDN.

Use ToString("0.0###########################").

It does work. I found it in duplicate of your question decimal ToString formatting which gives at least 1 digit, no upper limit

You can cast to string and then appen ".0" if there was no decimal point given

string sValue=doubleValue.ToString();
if(!sValue.Contains('.'))
    sValue+=".0";

EDIT: As mentioned in the comments '.' may not be the decimal seperator in the current culture. Refer to this article to retrieve the actual seperator if you want make your code save for this case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top