I have this double value:
var value = 52.30298270000003
and when I convert it to string, it losses its precision:
var str = string.Format("{0} some text...", value);
Console.WriteLine(str); // output: 52.3029827
The number of precision on my double value may be changed at run-time. How can I force the string.Format method to use all precision?
You want to use the R format specifier
From the MSDN
Result: A string that can round-trip to an identical number.
Supported by: Single, Double, and BigInteger.
Precision specifier: Ignored.
More information: The Round-trip ("R") Format Specifier.
String.Format("{0:R} some text...", value)
will give you
52.30298270000003 some text...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With