问题描述
因此,在我们实施的单元测试期间发现了这种奇怪的行为.
So this odd behavior was caught during a unit test we were implementing.
在 .NET Core 3.x 中,如果将接近 0 的负数四舍五入然后转换为字符串,则该字符串将显示-0";而不是 0.
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.
Framework 和 .NET Core 2.x 不会出现这种行为.
Framework and .NET Core 2.x don't exhibit this behavior.
double d = -.1;
Console.WriteLine(Math.Round(d,0));
这是 Core 中的一个新错误,还是一些奇怪的预期更改?
Is this a new bug within Core, or some strange intended change?
推荐答案
这是 .NET Core 3.0 中的一个突破性更改,以实现语言之间的一致性和 IEEE-754 合规性.
This was a breaking change in .NET Core 3.0, for consistency between languages and IEEE-754 compliance.
引用自 .NET Core 3.0 中的浮点解析和格式化改进:
ToString()、ToString(G") 和 ToString(R") 现在将返回最短的往返字符串.
上面的重点是往返".在这个变化之前,负零-0"没有正确往返,这是通过 修复双/单解析代码的组合来修复的#20707 和 单/双的往返字符串格式不往返 -0 #9883.
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.
这篇关于.NET Core 3 在舍入接近 0 的负数时返回 -0 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!