Java DecimalFormat 保留小数位及四舍五入的陷阱介绍
需求
业务需要导出的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 //
RoundingMode.HALF_EVEN
想了解HALF_EVEN,去官网API看了下
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 java.math.RoundingMode; import java.text.DecimalFormat; import java.util.*; public class LocalTest { //定义一个保留两位小数格式的 DecimalFormat 的变量 dfScale2 @Test public void testDecimalFormat() { DecimalFormat dfScale2 = new DecimalFormat("###.##"); System.out.println("dfScale2.getRoundingMode()=" + dfScale2.getRoundingMode()); System.out.println("dfScale2.format(1.125D)=" + dfScale2.format(1.125D)); System.out.println("dfScale2.format(1.135D)=" + dfScale2.format(1.135D)); System.out.println("dfScale2.format(1.145D)=" + dfScale2.format(1.145D)); System.out.println("dfScale2.format(1.225D)=" + dfScale2.format(1.225D)); System.out.println("dfScale2.format(1.235D)=" + dfScale2.format(1.235D)); System.out.println("dfScale2.format(1.245D)=" + dfScale2.format(1.245D)); System.out.println(); System.out.println("dfScale2.format(2.125D)=" + dfScale2.format(2.125D)); System.out.println("dfScale2.format(2.135D)=" + dfScale2.format(2.135D)); System.out.println("dfScale2.format(2.145D)=" + dfScale2.format(2.145D)); System.out.println("dfScale2.format(2.225D)=" + dfScale2.format(2.225D)); System.out.println("dfScale2.format(2.235D)=" + dfScale2.format(2.235D)); System.out.println("dfScale2.format(2.245D)=" + dfScale2.format(2.245D)); System.out.println(); System.out.println("dfScale2.format(3.125D)=" + dfScale2.format(3.125D)); System.out.println("dfScale2.format(3.135D)=" + dfScale2.format(3.135D)); System.out.println("dfScale2.format(3.145D)=" + dfScale2.format(3.145D)); System.out.println("dfScale2.format(3.225D)=" + dfScale2.format(3.225D)); System.out.println("dfScale2.format(3.235D)=" + dfScale2.format(3.235D)); System.out.println("dfScale2.format(3.245D)=" + dfScale2.format(3.245D)); System.out.println(); System.out.println("dfScale2.format(4.125D)=" + dfScale2.format(4.125D)); System.out.println("dfScale2.format(4.135D)=" + dfScale2.format(4.135D)); System.out.println("dfScale2.format(4.145D)=" + dfScale2.format(4.145D)); System.out.println("dfScale2.format(4.225D)=" + dfScale2.format(4.225D)); System.out.println("dfScale2.format(4.235D)=" + dfScale2.format(4.235D)); System.out.println("dfScale2.format(4.245D)=" + dfScale2.format(4.245D)); } }
dfScale2.getRoundingMode()=HALF_EVEN dfScale2.format(1.125D)=1.12 dfScale2.format(1.135D)=1.14 dfScale2.format(1.145D)=1.15 dfScale2.format(1.225D)=1.23 dfScale2.format(1.235D)=1.24 dfScale2.format(1.245D)=1.25 dfScale2.format(2.125D)=2.12 dfScale2.format(2.135D)=2.13 dfScale2.format(2.145D)=2.15 dfScale2.format(2.225D)=2.23 dfScale2.format(2.235D)=2.23 dfScale2.format(2.245D)=2.25 dfScale2.format(3.125D)=3.12 dfScale2.format(3.135D)=3.13 dfScale2.format(3.145D)=3.15 dfScale2.format(3.225D)=3.23 dfScale2.format(3.235D)=3.23 dfScale2.format(3.245D)=3.25 dfScale2.format(4.125D)=4.12 dfScale2.format(4.135D)=4.13 dfScale2.format(4.145D)=4.14 dfScale2.format(4.225D)=4.22 dfScale2.format(4.235D)=4.24 dfScale2.format(4.245D)=4.25
正确的代码测试RoundingMode.HALF_EVEN
突然发现自己忽略了一个事情,测试的参数都是用的double类型.想起来double类型不精准.但是侥幸心理以及知识不牢靠以为 3位小数应该影响不大吧.改了下代码,把参数改为BigDecimal类型
使用BigDecimal时,参数尽量传入字符串,要比传入double精准.
new BigDecimal("1.125")
@Test public void testDecimalFormat() { DecimalFormat dfScale2 = new DecimalFormat("###.##"); dfScale2.setRoundingMode(RoundingMode.HALF_EVEN); System.out.println("dfScale2.getRoundingMode()=" + dfScale2.getRoundingMode()); System.out.println("dfScale2.format(new BigDecimal(\"1.1251\"))=" + dfScale2.format(new BigDecimal("1.1251"))); System.out.println("dfScale2.format(new BigDecimal(\"1.1351\"))=" + dfScale2.format(new BigDecimal("1.1351"))); System.out.println("dfScale2.format(new BigDecimal(\"1.1451\"))=" + dfScale2.format(new BigDecimal("1.1451"))); System.out.println("dfScale2.format(new BigDecimal(\"1.2250\"))=" + dfScale2.format(new BigDecimal("1.2250"))); System.out.println("dfScale2.format(new BigDecimal(\"1.2350\"))=" + dfScale2.format(new BigDecimal("1.2350"))); System.out.println("dfScale2.format(new BigDecimal(\"1.2450\"))=" + dfScale2.format(new BigDecimal("1.2450"))); System.out.println("dfScale2.format(new BigDecimal(\"1.22501\"))=" + dfScale2.format(new BigDecimal("1.22501"))); System.out.println("dfScale2.format(new BigDecimal(\"1.23505\"))=" + dfScale2.format(new BigDecimal("1.23505"))); System.out.println("dfScale2.format(new BigDecimal(\"1.24508\"))=" + dfScale2.format(new BigDecimal("1.24508")));
dfScale2.getRoundingMode()=HALF_EVEN dfScale2.format(new BigDecimal("1.1251"))=1.13 dfScale2.format(new BigDecimal("1.1351"))=1.14 dfScale2.format(new BigDecimal("1.1451"))=1.15 dfScale2.format(new BigDecimal("1.2250"))=1.22 dfScale2.format(new BigDecimal("1.2350"))=1.24 dfScale2.format(new BigDecimal("1.2450"))=1.24 dfScale2.format(new BigDecimal("1.22501"))=1.23 dfScale2.format(new BigDecimal("1.23505"))=1.24 dfScale2.format(new BigDecimal("1.24508"))=1.25
结论
1、警觉doulbe的不精确所引起RoundingMode结果不稳定的问题,即使是四舍五入的模式,对double类型参数使用也会有不满足预期的情况.
2、使用数字格式化时,要注意默认RoundingMode模式是否是自己需要的.如果不是记得手动设置下.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持猪先飞。
相关文章
- 这篇文章主要介绍了如何利用java语言实现经典《复杂迷宫》游戏,文中采用了swing技术进行了界面化处理,感兴趣的小伙伴可以动手试一试...2022-02-01
java 运行报错has been compiled by a more recent version of the Java Runtime
java 运行报错has been compiled by a more recent version of the Java Runtime (class file version 54.0)...2021-04-01- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了Java如何发起http请求的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-31
- 说起C#和Java这两门语言(语法,数据类型 等),个人以为,大概有90%以上的相似,甚至可以认为几乎一样。但是在工作中,我也发现了一些细微的差别...2020-06-25
- 这篇文章主要介绍了解决Java处理HTTP请求超时的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-29
- 这篇文章主要介绍了java 判断两个时间段是否重叠的案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了超简洁java实现双色球若干注随机号码生成(实例代码),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-04-02
- 这篇文章主要介绍了Java生成随机姓名、性别和年龄的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-10-01
java 画pdf用itext调整表格宽度、自定义各个列宽的方法
这篇文章主要介绍了java 画pdf用itext调整表格宽度、自定义各个列宽的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-01-31- 这篇文章主要介绍了java正则表达式判断前端参数修改表中另一个字段的值,需要的朋友可以参考下...2021-05-07
Java使用ScriptEngine动态执行代码(附Java几种动态执行代码比较)
这篇文章主要介绍了Java使用ScriptEngine动态执行代码,并且分享Java几种动态执行代码比较,需要的朋友可以参考下...2021-04-15- 这篇文章主要介绍了Java开发实现人机猜拳游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-08-03
- 这篇文章主要介绍了Java List集合返回值去掉中括号('[ ]')的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-29
Java中lombok的@Builder注解的解析与简单使用详解
这篇文章主要介绍了Java中lombok的@Builder注解的解析与简单使用,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-01-06- 下面小编就为大家带来一篇java中String类型变量的赋值问题介绍。小编觉得挺不错的。现在分享给大家,给大家一个参考。...2016-03-28
Java 8 Stream 的终极技巧——Collectors 功能与操作方法详解
这篇文章主要介绍了Java 8 Stream Collectors 功能与操作方法,结合实例形式详细分析了Java 8 Stream Collectors 功能、操作方法及相关注意事项,需要的朋友可以参考下...2020-05-20- 这篇文章主要介绍了Java线程池中的各个参数如何合理设置操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-19
- 在Java中,我们可以利用多线程来最大化地压榨CPU多核计算的能力,下面这篇文章主要给大家介绍了关于java中多线程与线程池基本使用的相关资料,需要的朋友可以参考下...2021-09-13