C# 将 20 位精度 double 转换为字符串并再次返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/611552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 10:15:00  来源:igfitidea点击:

C# Converting 20 digit precision double to string and back again

c#doubleprecisiontostring

提问by

In C#. I have a double (which I've extracted from a database) that has 20 digit precision. In Visual Studio (using QuickWatch) I can see the value of the double to be = 0.00034101243963859839.

在 C# 中。我有一个精度为 20 位的 double(我从数据库中提取的)。在 Visual Studio(使用 QuickWatch)中,我可以看到 double 的值为 = 0.00034101243963859839。

I want to display this value in a textbox and then have it be the same value when I take it out and convert it back into a double. But I always lose the last two digits

我想在文本框中显示这个值,然后在取出它并将其转换回双精度值时让它成为相同的值。但我总是输最后两位数

I've tried the following:

我尝试了以下方法:

double d = 0.00034101243963859839;
string s = d.ToString();
string s2 = d.ToString("F20");
string s3 = d.ToString("0.00000000000000000000"); -- 20 0's
string s4 = (d*100d).ToString();

In these cases:

在这些情况下:

s  = 0.000341012439638598
s2 = 0.00034101243963859800
s3 = 0.00034101243963859800
s4 = 0.0341012439638598

I want to be able to do the following:

我希望能够做到以下几点:

double d = 0.00034101243963859839;
string s = d.ToString();
//...
double d2 = double.Parse(s);
if(d == d2)
{
  //-- Success
}

Is there any way to keep those last two digits of precision??

有没有办法保持最后两位精度?

采纳答案by Joel Coehoorn

Use the "R" numeric format string:

使用“R”数字格式字符串

double d = 0.00034101243963859839;
string s = d.ToString("R");
//...
double d2 = double.Parse(s);
if(d == d2)
{
  //-- Success
}

The Rstands for "round-trip". From the linked document:

R代表“往返”。从链接的文件:

This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value.

此格式仅支持 Single 和 Double 类型。往返说明符保证转换为字符串的数值将被解析回相同的数值。

As an aside, I suspect there is no way to keep those last two digits. There's only so much precision available, and I doubt they ever make it into din the first place. But you can make sure your string at least reads back what you do have correctly.

顺便说一句,我怀疑没有办法保留最后两位数字。可用的精度只有这么多,我怀疑他们一开始就没有做到d。但是您可以确保您的字符串至少可以正确读取您所做的内容。

If you really need the additional precision, you might try using a decimalinstead.

如果你真的需要额外的精度,你可以尝试使用 adecimal代替。

回答by Thorsten S.

No. Besides the fact that double is binary and therefore not good for decimal values, doubles have a maximum of 15/16 decimal digits (53 bits). decimal has a maximum of 28/29 digits (96 bit), so it would be ok to use it. If you have higher precisions in the database, you need an own bignum class for C#, look in stackoverflow for implementations.

不是。除了 double 是二进制的,因此不适用于十进制值这一事实之外,double 最多有 15/16 位十进制数字(53 位)。十进制最多有 28/29 位(96 位),所以可以使用它。如果你在数据库中有更高的精度,你需要一个自己的 C# bignum 类,在 stackoverflow 中查找实现。