Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.format(format,doubleValue) , precision lost

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?

like image 358
amiry jd Avatar asked May 04 '26 12:05

amiry jd


1 Answers

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...
like image 105
Scott Chamberlain Avatar answered May 07 '26 01:05

Scott Chamberlain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!