How do I prevent my double value from being rounded when converting to a string? I have tried both Convert.ToString
and ToString()
with the same result.
For example my double may look something like 77.987654321
, and the two strings conversions convert to to 77.98765
. I need to keep the precision of the value as is.
Try something like
See also The Round-trip ("R") Format Specifier:
By default the
.ToString()
method ofDouble
returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the "G17" format specifier to the method.Sourced from the MSDN docs:
Jon Skeet's
DoubleConverter
class has aToExactString()
method which will return the exact value of the double as a string.http://www.yoda.arachsys.com/csharp/DoubleConverter.cs
No guarantees, but try ToString("R").
I would assume that the main answer for rounding away the last two digits, is to hide numerical instability/rounding due to float/double finite precision.
Example with no rounding:
Looks a bit strange in the last 3 digits, right?
Example with rounding:
Look "perfect", right?
:-)