C# 的 System.Single 精度

发布于 2024-12-11 22:12:45 字数 220 浏览 0 评论 0原文

在下面的示例中,数字 12345678.9 在转换为字符串时会失去准确性,因为它变为 1.234568E+07。我只需要一种方法来保持大浮点数的准确性。谢谢。

Single sin1 = 12345678.9F;
String str1 = sin1.ToString();
Console.WriteLine(str1); // displays 1.234568E+07

In the example below the number 12345678.9 loses accuracy when it's converted to a string as it becomes 1.234568E+07. I just need a way to preserve the accuracy for large floating point numbers. Thanks.

Single sin1 = 12345678.9F;
String str1 = sin1.ToString();
Console.WriteLine(str1); // displays 1.234568E+07

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤者何惧 2024-12-18 22:12:45

如果您想保留十进制数字,您应该使用System.Decimal。就这么简单。 System.SingleSystem.Double 根据文档:

默认情况下,单个值仅包含 7 位小数位精度,但内部最多保留 9 位数字。

当您将其转换为字符串时,您不仅丢失了信息,还丢失了第一行的信息。这不仅仅是因为您使用的是 float 而不是 double,而是因为您使用的是浮动二进制点数。

无论您使用多大的类型,十进制数 0.1 都无法在二进制浮点系统中准确表示...

请参阅我的文章 浮动二进制小数点浮动小数点点了解更多信息。当然,您可能应该使用double甚至float并且不关心精度损失 - 这取决于您的情况正在努力代表。但如果您真的确实关心保留十进制数字,那么请使用基于十进制的类型。

If you want to preserve decimal numbers, you should use System.Decimal. It's as simple as that. System.Single is worse than System.Double in that as per the documentation:

By default, a Single value contains only 7 decimal digits of precision, although a maximum of 9 digits is maintained internally.

You haven't just lost information when you've converted it to a string - you've lost information in the very first line. That's not just because you're using float instead of double - it's because you're using a floating binary point number.

The decimal number 0.1 can't be represented accurately in a binary floating point system no matter how big you make the type...

See my articles on floating binary point and floating decimal point for more information. Of course, it's possible that you should be using double or even float and just not caring about the loss of precision - it depends on what you're trying to represent. But if you really do care about preserving decimal digits, then use a decimal-based type.

草莓味的萝莉 2024-12-18 22:12:45

你不能。就这么简单。在内存中,您的号码是 12345679。请尝试以下代码。

Single sin1 = 12345678.9F;
String str1 = sin1.ToString("r"); // Shows "all" the number
Console.WriteLine(sin1 == 12345679); // true
Console.WriteLine(str1); // displays 12345679

从技术上讲,r 的意思是(引用自 MSDNround-trip:结果:一个可以往返到相同数字的字符串。所以实际上它并没有显示所有的小数。它仅显示将其与 Single 的其他可能值区分开来所需的所有小数。如果您想显示所有小数,请使用F20

如果您想要更高的精度,请使用double,或者更好地使用decimalfloat 具有它所具有的精度。正如我们在意大利所说的“Non puoi spremere sangue da una rapa”(你无法从萝卜中挤出血)

You can't. Simple as that. In memory your number is 12345679. Try the code below.

Single sin1 = 12345678.9F;
String str1 = sin1.ToString("r"); // Shows "all" the number
Console.WriteLine(sin1 == 12345679); // true
Console.WriteLine(str1); // displays 12345679

Technically r means (quoting from MSDN) round-trip: Result: A string that can round-trip to an identical number. so in reality it isn't showing all the decimals. It's only showing all the decimals needed to distinguish it from other possible values of Single. If you want to show all the decimals use F20.

If you want more precision use double or better use decimal. float has the precision that it has. As we say in Italy "Non puoi spremere sangue da una rapa" (You can't squeeze blood from a turnip)

月下客 2024-12-18 22:12:45

您还可以为您的目的编写一个 IFormatProvider - 但除非您使用不同的类型,否则精度不会提高。
本文可能会有所帮助 - http://www.csharp-examples.net/string-format -双/

You could also write an IFormatProvider for your purpose - but the precision doesn't get any better unless you use a different type.
this article may help - http://www.csharp-examples.net/string-format-double/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文