简体   繁体   English

.NET Core 3 在舍入接近 0 的负数时返回 -0 字符串

[英].NET Core 3 returns -0 string when a near 0 negative number is rounded

So this odd behavior was caught during a unit test we were implementing.因此,在我们实施的单元测试中发现了这种奇怪的行为。

In .NET Core 3.x, if a near 0 negative number is rounded and then converted to a string, the string will display "-0" instead of 0.在 .NET Core 3.x 中,如果将接近 0 的负数四舍五入然后转换为字符串,则该字符串将显示“-0”而不是 0。

Framework and .NET Core 2.x don't exhibit this behavior.框架和 .NET Core 2.x 不会出现这种行为。

double d = -.1;
Console.WriteLine(Math.Round(d,0));

.NET Core Fiddle .NET 核心小提琴

Framework Fiddle框架小提琴

Is this a new bug within Core, or some strange intended change?这是 Core 中的一个新错误,还是一些奇怪的预期更改?

This was a breaking change in .NET Core 3.0, for consistency between languages and IEEE-754 compliance.这是 .NET Core 3.0 中的一项重大更改,以实现语言之间的一致性和 IEEE-754 合规性。

Quoting from Floating-Point Parsing and Formatting improvements in .NET Core 3.0 :引用.NET Core 3.0 中的浮点解析和格式化改进

ToString(), ToString("G"), and ToString("R") will now return the shortest roundtrippable string. ToString()、ToString("G") 和 ToString("R") 现在将返回最短的可往返字符串。

The emphasis in the above is on " roundtrippable ".上面的重点是“可往返”。 Before this change, the negative zero "-0" did not roundtrip correctly, which was fixed by the combination of Fixing up the Double/Single parsing code to be correct #20707 and Roundtrip string format for single/double doesn't roundtrip -0 #9883 .在此更改之前,负零“-0”没有正确往返,这是通过将 Double/Single 解析代码修复为正确 #20707和 Roundtrip string format for single/double doesn't roundtrip -0组合修复的第9883章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM