JAVA DecimalFormat 保留小数位以及四舍五入的陷阱

本文探讨了Java中使用DecimalFormat进行数字格式化的细节,特别是不同舍入模式的影响,重点讲解了HALF_EVEN模式的特性及正确应用。

需求

业务需要导出的Excel的数字内容保留两位小数,并且四舍五入

代码实现

百度一圈所抄袭的代码

DecimalFormat dfScale2 = new DecimalFormat("###.##");
dfScale2.format(1.125D);

发现问题

导出数据很诡异.不是所有数据都是如所想的四舍五入.
经过排查最终发现是RoundingMode的问题,应该使用HALF_UP,
DecimalFormat 默认使用的是HALF_EVEN

DecimalFormat dfScale2 = new DecimalFormat("###.##");
System.out.println("dfScale2.getRoundingMode()=" + dfScale2.getRoundingMode());
//输出结果
dfScale2.getRoundingMode()=HALF_EVEN

@Test
    public void decimalFormatTest() {
   
   
        DecimalFormat decimalFormat_HAFL_EVEN = new DecimalFormat("#.##");
        decimalFormat_HAFL_EVEN.setRoundingMode(RoundingMode.HALF_EVEN);
        System.out.println("decimalFormat_HAFL_EVEN.format(new BigDecimal(12.245))=" + decimalFormat_HAFL_EVEN.format(new BigDecimal(12.245D)));
        System.out.println("decimalFormat_HAFL_EVEN.format(new BigDecimal(\"12.245\"))=" + decimalFormat_HAFL_EVEN.format(new BigDecimal("12.245")));
//        decimalFormat_HAFL_EVEN.format(new BigDecimal(12.245))=12.24
//        decimalFormat_HAFL_EVEN.format(new BigDecimal("12.245"))=12.24

        DecimalFormat decimalFormat_HALF_UP = new DecimalFormat("#.##");
        decimalFormat_HALF_UP.setRoundingMode(RoundingMode.HALF_UP);
        System.out.println("decimalFormat_HALF_UP.format(new BigDecimal(12.245))=" + decimalFormat_HALF_UP.format(new BigDecimal(12.245D)));
        System.out.println("decimalFormat_HALF_UP.format(new BigDecimal(\"12.245\"))=" + decimalFormat_HALF_UP.format(new BigDecimal("12.245")));
//        decimalFormat_HALF_UP.format(new BigDecimal(12.245))=12.24
//        decimalFormat_HALF_UP.format(new BigDecimal("12.245"))=12.25
    }

RoundingMode.HALF_EVEN

想了解HALF_EVEN,去官网API看了下
https://docs.oracle.com/javase/8/docs/api/java/math/RoundingMode.html
HALF_EVEN 被舍位是5(如保留两位小数的2.115),后面还有非0值进1(如保留两位小数的2.11500001 格式化为2.12),5后面没有数字或者都是0时,前面是偶数则舍,是奇数则进1,目标是让被舍前一位变为偶数.

CEILING 向更大的值靠近
Rounding mode to round towards positive infinity.
DOWN向下取整
Rounding mode to round towards zero.
FLOOR 向更小的值靠近
Rounding mode to round towards negative infinity.
HALF_DOWN 五舍六入
Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
HALF_EVEN
Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
HALF_UP 四舍五入
Rounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
UNNECESSARY 设置这个模式,对于精确值格式化会抛出异常
Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
UP 向远离数字0进行进位.
Rounding mode to round away from zero.
在这里插入图片描述

错误的代码测试RoundingMode.HALF_EVEN

为了更好的理解HALF_EVEN,写了些测试代码但是发现自己更迷惘了…搞不清楚到底HALF_EVEN是什么机制进舍…输出结果的尾数很不规律.

import java.math.BigDecimal;
import 
评论 12
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值