今天在写一个JAVA程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result
发现报错的语句是:

1
foo.divide(bar));

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

1
foo.divide(bar, 2, BigDecimal.ROUND_HALF_UP);

注意这个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的模式

Tonitech版权所有 | 转载请注明出处: http://www.tonitech.com/2327.html

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法的更多相关文章

  1. BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result的解决办法

    BigDecimal除法运算出现java.lang.ArithmeticException: Non-terminating decimal expansion; no exact represent ...

  2. 除法运算时的一个常见异常之java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

    一.背景 今天在计算库存消耗百分比(消耗的库存/总库存)的时候遇到了一个错误,java.lang.ArithmeticException: Non-terminating decimal expans ...

  3. Bigdecimal: Non-terminating decimal expansion; no exact representable decimal result.

    做除法没有指定保留小数点后几位,就会抛出此异常. 因为会除不尽 Non-terminating decimal expansion; no exact representable decimal re ...

  4. Non-terminating decimal expansion; no exact representable decimal result.

    Non-terminating decimal expansion; no exact representable decimal result.  翻译为:非终止十进制扩展; 没有确切的可表示的小数 ...

  5. 【异常】java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

    异常原因:没有指定数据精度导致数据运算无法正常结算 如执行下面的除法: ).divide(BigDecimal.valueOf()).intValue(); 指定精度后就可以了: ).divide(B ...

  6. 【java错误】Non-terminating decimal expansion; no exact representable decimal result

    问题描述 意思是“无法结束的除法表达式:没有精确的除结果”.当时输入的10/3,结果应该是3.3333....333. 下面这种处理方式有问题. BigDecimal num3 = new BigDe ...

  7. Java_类似java.lang.VerifyError: Expecting a stackmap frame at branch target 22 in method的解决方法

    报异常的方法内使用了Java 7的新特性:自动资源释放,类似于try(){},即在try后面跟一括号,在括号里面对一些资源赋值,try里面的代码块执行完毕之后会自动释放try后面的括号中声明的资源. ...

  8. BigDecimal不整除的一个异常java.lang.ArithmeticException

    转载地址:http://blog.csdn.net/jobjava/article/details/6764623 金额的数据类型是BigDecimal 通过BigDecimal的divide方法进行 ...

  9. java.lang.ArithmeticException: / by zero

随机推荐

  1. 【转】 聚类算法-Kmeans算法的简单实现

    1. 聚类与分类的区别: 首先要来了解的一个概念就是聚类,简单地说就是把相似的东西分到一组,同 Classification (分类)不同,对于一个 classifier ,通常需要你告诉它“这个东西 ...

  2. mismatch详解

    mismatch到底只是一个碱基替换,还是也包括了插入缺失? If two sequences in an alignment share a common ancestor, mismatches ...

  3. English trip V1 - B 17. Giving Information 提供信息 Teacher:Taylor Key: Person Information

    In this lesson you will learn to say your phone number and address.  这节课讲学习说你的手机号码和地址. 课上内容(Lesson) ...

  4. patch-test-and-proc

    实验环境 Ubuntu 14.04.5 LTS Linux - 4.15.6 为单个文件进程补丁操作 在桌面 Desktop 建立文件夹 patch ,作为实验用,然后进入patch 文件夹.建立测试 ...

  5. apiCloud 浏览图片

    点击链接查看api详情 https://docs.apicloud.com/Client-API/Func-Ext/photoBrowser var photoBrowser = api.requir ...

  6. 解决无法安装Microsoft .Net Framework 3.5

    如果解决不了,试试我的方法吧,我也在网上找了好久,最终在本地解决了 所需工具:dism,和Net Framework 3.5,已经打包 链接:https://pan.baidu.com/s/1nKok ...

  7. spring boot(十五)spring boot+thymeleaf+jpa增删改查示例

    快速上手 配置文件 pom包配置 pom包里面添加jpa和thymeleaf的相关包引用 <dependency> <groupId>org.springframework.b ...

  8. Mac实现远程服务器登录管理

    在MAC上还没发现xshell一样的终端工具.不过mac的终端命令,可以写好多脚本来管理. 首先想到的就是把服务器IP及密码使用shell管理起来,以便下次登录的时候不用在重新输入,或者只输入代号就行 ...

  9. 前端页面兼容ie8解决方法

    一.通用兼容文件的引用: 1.HTML5标签兼容方案:html5shiv.js GitHub地址:https://github.com/aFarkas/html5shiv/ IE8不支持HTML5的新 ...

  10. [luogu P2324] [SCOI2005]骑士精神

    [luogu P2324] [SCOI2005]骑士精神 题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1 ...