C# 的 System.Single 精度
在下面的示例中,数字 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想保留十进制数字,您应该使用
System.Decimal
。就这么简单。System.Single
比System.Double
根据文档:当您将其转换为字符串时,您不仅丢失了信息,还丢失了第一行的信息。这不仅仅是因为您使用的是
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 thanSystem.Double
in that as per the documentation: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 ofdouble
- 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 evenfloat
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.你不能。就这么简单。在内存中,您的号码是 12345679。请尝试以下代码。
从技术上讲,
r
的意思是(引用自 MSDN)round-trip:结果:一个可以往返到相同数字的字符串。
所以实际上它并没有显示所有的小数。它仅显示将其与Single
的其他可能值区分开来所需的所有小数。如果您想显示所有小数,请使用F20
。如果您想要更高的精度,请使用
double
,或者更好地使用decimal
。float
具有它所具有的精度。正如我们在意大利所说的“Non puoi spremere sangue da una rapa”(你无法从萝卜中挤出血)You can't. Simple as that. In memory your number is 12345679. Try the code below.
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 ofSingle
. If you want to show all the decimals useF20
.If you want more precision use
double
or better usedecimal
.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)您还可以为您的目的编写一个 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/