0

I have piece of code similar to the following:

public class Program
{
    public static void Main()
    {
        double value = 1035.1421666793442;
        string converted = value.ToString("F15", System.Globalization.CultureInfo.InvariantCulture);

        converted.Dump();
    }
}

I want to convert this double to a string with all decimals. But the converted string always looks like this: 1035.142166679340000. So the value was rounded before formatting to "F15".

You can reproduce this with the following Fiddle: https://dotnetfiddle.net/ynlsYW

Richy1989
  • 97
  • 7
  • 4
    This is caused by floating point precision that is lost. Use `decimal` instead. – Patrick Hofman Feb 03 '16 at 12:41
  • Patrick beat me to it, but since I already made a code sample I figured I might as well share it https://dotnetfiddle.net/ifuD7k –  Feb 03 '16 at 12:43
  • Thanks for your help. My problem is i can not change the type of value. It has to be double. Afterwards i can convert or transform it as much as i want. So is there there any chance to get all decimals out of it starting at a double value? – Richy1989 Feb 03 '16 at 12:57
  • https://msdn.microsoft.com/en-us/library/system.double(v=vs.110).aspx "A Double value has up to 15 decimal digits of precision". "1035.1421666793442" requires 17 digits of precision, so it simply doesn't fit in to a double. As far as I'm aware you can't get back data that hasn't been stored. –  Feb 03 '16 at 13:03

0 Answers0