string.format(format,doubleValue),精度丢失

age*_*t47 3 c# double string.format double-precision

我有这个double价值:

var value = 52.30298270000003
Run Code Online (Sandbox Code Playgroud)

当我把它转换成它时string,它失去了它的精度:

var str = string.Format("{0} some text...", value);
Console.WriteLine(str); // output: 52.3029827
Run Code Online (Sandbox Code Playgroud)

我的值的精度double可能会在运行时更改.如何强制该string.Format方法使用所有精度

Sco*_*ain 6

您想使用R格式说明符

来自MSDN

结果:可以往返到相同数字的字符串.

支持者:Single,Double和BigInteger.

精度说明符:忽略.

更多信息:往返("R")格式说明符.

String.Format("{0:R} some text...", value)
Run Code Online (Sandbox Code Playgroud)

会给你

52.30298270000003 some text...
Run Code Online (Sandbox Code Playgroud)