为什么';t java.math.RoundingMode是否带有半个奇数舍入?

为什么';t java.math.RoundingMode是否带有半个奇数舍入?,java,math,Java,Math,带有半偶数模式,在等距情况下将数字舍入到最近的偶数邻居,但为什么不带有半奇数模式 在Java中实现半舍五入的最简单方法是什么?我希望这能对您有所帮助 建议的解决办法是: public static int RoundToNearestRoundHalfToOdd(decimal value) { // First round half toward positive infinity. Then if the fraction // part of the original va

带有半偶数模式,在等距情况下将数字舍入到最近的偶数邻居,但为什么不带有半奇数模式

在Java中实现半舍五入的最简单方法是什么?

我希望这能对您有所帮助

建议的解决办法是:

public static int RoundToNearestRoundHalfToOdd(decimal value)
{
    // First round half toward positive infinity. Then if the fraction
    // part of the original value is 0.5 and the result of rounding is
    // even, then subtract one.
    var temp = (int)Math.Floor(value + 0.5m);
    if (value - Math.Floor(value) == 0.5m && temp % 2 == 0)
        temp -= 1;
    return temp;
}
它是C语言的,但我想你可以把它转换成Java语言

另外,为了帮助您完成任务,您可以在Java SDK中查看该方法的源代码,在那里一切都在进行。

我希望这能对您有所帮助

建议的解决办法是:

public static int RoundToNearestRoundHalfToOdd(decimal value)
{
    // First round half toward positive infinity. Then if the fraction
    // part of the original value is 0.5 and the result of rounding is
    // even, then subtract one.
    var temp = (int)Math.Floor(value + 0.5m);
    if (value - Math.Floor(value) == 0.5m && temp % 2 == 0)
        temp -= 1;
    return temp;
}
它是C语言的,但我想你可以把它转换成Java语言

另外,为了帮助完成任务,您可以在Java SDK中查看该方法的源代码,在那里所有事情都在发生。

为什么?可能会看到。为什么?可能会看到。