Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

BigDecimal文档里,建议对保留规则设置为ROUND_HALF_EVEN #2129

Closed
sunquan123 opened this issue Aug 10, 2023 · 2 comments
Closed
Labels
enhancement New feature or request or suggestion

Comments

@sunquan123
Copy link

文档里BigDecimalUtil类选择的保留规则是RoundingMode.HALF_UP,即四舍五入。代码部分如下:

/**
     * 提供精确的小数位四舍五入处理。
     *
     * @param v     需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = BigDecimal.valueOf(v);
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
    }

建议修改为BigDecimal.ROUND_HALF_EVEN,也就是RoundingMode.HALF_EVEN,即四舍六入五成双(为什么会发现:做项目时发现不同的写法,去查资料看了)。
Snipaste_2023-08-10_21-12-40
我就把百度的四舍六入五成双规则贴过来了:
对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”,这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲:
(1)5前为奇数,舍5入1;
(2)5前为偶数,舍5不进(0是偶数)。
推荐使用四舍六入五成双规则的原因:
从统计学的角度,“四舍六入五成双”比“四舍五入”要科学,在大量运算时,它使舍入后的结果误差的均值趋于零,而不是像四舍五入那样逢五就入,导致结果偏向大数,使得误差产生积累进而产生系统误差,“四舍六入五成双”使测量结果受到舍入误差的影响降到最低。

@Snailclimb
Copy link
Owner

文档里BigDecimalUtil类选择的保留规则是RoundingMode.HALF_UP,即四舍五入。代码部分如下:

/**
     * 提供精确的小数位四舍五入处理。
     *
     * @param v     需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static double round(double v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = BigDecimal.valueOf(v);
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, RoundingMode.HALF_UP).doubleValue();
    }

建议修改为BigDecimal.ROUND_HALF_EVEN,也就是RoundingMode.HALF_EVEN,即四舍六入五成双(为什么会发现:做项目时发现不同的写法,去查资料看了)。 Snipaste_2023-08-10_21-12-40 我就把百度的四舍六入五成双规则贴过来了: 对于位数很多的近似数,当有效位数确定后,其后面多余的数字应该舍去,只保留有效数字最末一位,这种修约(舍入)规则是“四舍六入五成双”,也即“4舍6入5凑偶”,这里“四”是指≤4 时舍去,"六"是指≥6时进上,"五"指的是根据5后面的数字来定,当5后有数时,舍5入1;当5后无有效数字时,需要分两种情况来讲: (1)5前为奇数,舍5入1; (2)5前为偶数,舍5不进(0是偶数)。 推荐使用四舍六入五成双规则的原因: 从统计学的角度,“四舍六入五成双”比“四舍五入”要科学,在大量运算时,它使舍入后的结果误差的均值趋于零,而不是像四舍五入那样逢五就入,导致结果偏向大数,使得误差产生积累进而产生系统误差,“四舍六入五成双”使测量结果受到舍入误差的影响降到最低。

牛啊牛啊,学到了👍

@Snailclimb Snailclimb added the enhancement New feature or request or suggestion label Aug 14, 2023
@openSupporter237
Copy link

也称为银行家舍入法

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request or suggestion
Projects
None yet
Development

No branches or pull requests

3 participants