1

I want to convert following 3 double values to string.

Double value            converted string value
1                       1
1.200                   1.200
1.666                   1.666

I want my output string values in same format as double. Means in above values 1 doesn't have any decimal value, so it will remain same. In case of 1.200 it will give me string as 1.200 and same for 1.666. However I tried .ToString() method but it truncated the ZERO value for 1.200 value. But i don't want this. I want Actual values in string format.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
Tikam Sangwani
  • 305
  • 1
  • 10
  • 21
  • I don't think you want "Actual" values (i.e. 1.2000000000001) – crashmstr May 21 '14 at 17:44
  • 3
    "I want my output string values in same format as double" - do you mean you want to differentiate between original string inputs of 1.20 and 1.200? If so, `double` is the wrong type to use. If that's not what you mean, you really need to work on the clarity of your question. We have no idea where the data's coming from, or what you mean by "in same format as double". – Jon Skeet May 21 '14 at 17:45
  • If you want to control the output of a double, use formatting. For instance `string.Format("{0:0.000}", myDouble)` method will format a double to have at least one sign on the left and 3 places on the right of the decimal point. If you want to output the exact value in the variable declaration, and make a difference between `double v1 = 1;` and `double v2 = 1.000;`, then `double` is not your data type - the language makes no difference between `1` and `1.000`, unless both are strings. – Ivaylo Slavov May 21 '14 at 17:49
  • Hi @JonSkeet, My meaning is i do not want to truncated zero values after decimal places. Actually these values are coming from database. So i want that these values remain same(same decimal places) after string conversion. – Tikam Sangwani May 21 '14 at 17:50
  • Hi @lvaylo Slavov, i just want to print these values on a report, but in same format as they are. I am getting these values from database using nHibernate and in my Entity class, property for these fields declared as double. – Tikam Sangwani May 21 '14 at 17:52
  • 2
    C# doesn't distinguish between 1.2 and 1.200 and 1.20000 if this is stored to a double type. They are all equal if you create 3 double variables and assign those values to them and print them out or perform comparison tests against each other you'll see this. So the trailing zeros aren't really part of the value when using a double type and you can only print them with specialized formatting. (edit: Ivaylo said basically the same thing and I didn't notice it before posting my comment. Oops) – Tim Cavanaugh May 21 '14 at 18:01
  • It sounds like you should be reading the value as a `decimal` instead of a `double`. – Jon Skeet May 21 '14 at 18:27

2 Answers2

1

I admit that I don't like the idea of doing this, but I wrote an extension method for the Double type to format it. Feel free to change the funny names. :)

public static class StupidDouble
{
    public static string StupidFormat(this double theValue)
    {
        // geth the double value to three decimal points
        string s = string.Format("{0:0.000}", theValue);

        // get the whole number value
        int check = (int) Math.Truncate(theValue);

        // if the format of the int matches the double, display without decimal places
        if (string.Format("{0:0.000}", check) == s)
        {
            s = check.ToString();
        }

        return s;
    }
}
David Crowell
  • 3,711
  • 21
  • 28
  • Note that this won't handle the full range of a Double. Hopefully your data has sane values. – David Crowell May 21 '14 at 18:05
  • I think the only stupid thing with this approach is the name you chose. Maybe, if the format came as a parameter (rather being hard-coded), I'd even consider using such method in my daily work (not joking) :) – Ivaylo Slavov May 21 '14 at 19:15
1

C#, or any other language (including SQL), does not make differences in the values of floating-point types (float, double) based on their representation in your code.

This means that the compiler does not make a difference between:

double v1 = 1;

and

double v2 = 1.000;

Neither does the database where you say (in the comments) you get these values from.

The reason databases display decimals in a certain way is because of a pre-defined formatting applied to the result set. This could either be defined by the database tool you use, or depend on your locale system settings. Anyway, the .NET framework provides you with capabilities to explicitly format your data.

You need to decide which formatting suits your needs best an use it. For instance this will format the number with 4 decimal places after the dot:

String.Format("{0:0.0000}", v1); // outputs 1.0000
String.Format("{0:0.0000}", v2); // outputs 1.0000 too

If you already know your desired format, adjust the formatting string (the first argument that looks like "{0:0000}" in a way that best corresponds to your requirements.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108
  • 1
    This is *not* true for `decimal` in C#. 1.00m and 1.0000m are different - try printing them out. It's true for `double` and `float`, but not `decimal`. – Jon Skeet May 21 '14 at 18:26
  • @JonSkeet, yes, indeed it appears to be so (I admit, just I really tried this). I knew `decimal` was more special in its representation than the floating-point types, but I did not suspect the difference is sensible that far. Thanks for the hint, fixed the answer now. – Ivaylo Slavov May 21 '14 at 18:33
  • 1
    Bear in mind that `decimal` is *also* a floating point type. It's just that it's a floating *decimal* point type, not a floating *binary* point type. – Jon Skeet May 21 '14 at 18:33