gpt4 book ai didi

c# - Math.Round 返回奇数向上舍入但偶数向下舍入

转载 作者:太空狗 更新时间:2023-10-30 00:01:24 25 4
gpt4 key购买 nike

我正在尝试使用数学回合找到一个 float 我发现了以下内容

0.5 --> 0
1.5 --> 2
2.5 --> 2
3.5 --> 4

等等。我相信这是由于浮点错误造成的,但不太确定是如何造成的。我怎样才能解决这个问题,以便偶数正确舍入?

最佳答案

来自 documentation ;

The integer nearest a. If the fractional component of a is halfway between two integers, one of which is even and the other odd, then the even number is returned. Note that this method returns a Double instead of an integral type.

Math.Round 方法有一些 overloads这需要 MidpointRounding作为一个参数,您可以指定舍入值,如果它在两个数字之间。

AwayFromZero 

When a number is halfway between two others, it is rounded toward the nearest number that is away from zero.

ToEven

When a number is halfway between two others, it is rounded toward the nearest even number.

关于c# - Math.Round 返回奇数向上舍入但偶数向下舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com