zoukankan      html  css  js  c++  java
  • java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result

    今天在调试一个JAVA程序的时候出现了如下异常错误信息:

    java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result

    发现报错的语句是:

    1
    fun.divide(bar));

    原来JAVA中如果用BigDecimal做除法的时候一定要在divide方法中传递第二个参数,定义精确到小数点后几位,否则在不整除的情况下,结果是无限循环小数时,就会抛出以上异常。
    解决方

    1
    fun.divide(bar, 2, BigDecimal.ROUND_HALF_DOWN);

    注意这个divide方法有两个重载的方法,一个是传两个参数的,一个是传三个参数的:

    两个参数的方法:

    @param divisor value by which this {@code BigDecimal} is to be divided. 传入除数

    @param roundingMode rounding mode to apply. 传入round的模式

    三个参数的方法:

    @param divisor value by which this {@code BigDecimal} is to be divided. 传入除数
    @param scale scale of the {@code BigDecimal} quotient to be returned. 传入精度
    @param roundingMode rounding mode to apply. 传入round的模式

  • 相关阅读:
    带着萌新看springboot源码
    学习springboot
    第一次来博客园
    第2章 排版样式
    第1章 Bootstrap介绍
    tab左右箭头切换
    $()下的常用方法2
    前端性能优化----yahoo前端性能团队总结的35条黄金定律
    tab
    $()下的常用方法
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/12300541.html
Copyright © 2011-2022 走看看