转载地址:http://blog.csdn.net/jobjava/article/details/6764623

金额的数据类型是BigDecimal

通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常的,异常如下:Java.lang.ArithmeticException:

Non-terminating decimal expansion; no exact representable decimal result. at java.math.BigDecimal.divide(Unknown Source)



应用场景:一批中供客户的单价是1000元/年,如果按月计算的话1000/12=83.3333333333....



解决之道:就是给divide设置精确的小数点divide(xxxxx,2, BigDecimal.ROUND_HALF_EVEN)

在 java中, 四舍五入通过 BigDecimal 来实现。一定要注意:BigDecimal is Immutable。

也就是跟String一样,对前一个的修改,比如setScale(), add()等都会返回一个新的BigDecimal.

四舍五入舍入模式是 BigDecimal.ROUND_HALF_UP 

BigDecimal定义了一下舍入模式,只有在作除法运算或四舍五入时才用到舍入模式,

下面简单介绍,详细请查阅J2se API文档

static int

ROUND_DOWN

Rounding mode to round towards zero.

向零方向舍入

static int

ROUND_FLOOR

Rounding mode to round towards negative infinity.

向负无穷方向舍入

static int

ROUND_HALF_DOWN

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant,

in which case round down.

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入,例如1.55保留一位

小数结果为1.5

static int

ROUND_HALF_EVEN

Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant,

in which case, round towards the even neighbor.

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,

使用ROUND_HALF_UP,如果是偶数,使用ROUND_HALF_DOWN

static int

ROUND_HALF_UP

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant,

in which case round up.

向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位

小数结果为1.6

static int

ROUND_UNNECESSARY

Rounding mode to assert that the requested operation has an exact result, hence no

rounding is necessary.

计算结果是精确的,不需要舍入模式

static int

ROUND_UP

Rounding mode to round away from zero.

向远离0的方向舍入

BigDecimal不整除的一个异常java.lang.ArithmeticException的更多相关文章

  1. 【异常】Phoenix异常:java.lang.ArithmeticException: Rounding necessary

    1 异常sql upsert into WMBIGDATA.ODS_ES_CHARGING_STATION(id,evcosType,address,serviceTel,supportOrder,o ...

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

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

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

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

  4. java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result异常的解决方法

    今天在写一个JAVA程序的时候出现了异常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact repr ...

  5. maven(多个模块)项目 部署 开发环境 问题处理历程【异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE】

    maven(多个模块)项目 部署 开发环境 问题处理历程[异常Name jdbc is not bound in this Context 异常java.lang.NoSuchMethodE] 201 ...

  6. 项目启动异常,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo ...

  7. 异常:java.lang.LinkageError: loader constraint violation: when resolving interface method

    异常:java.lang.LinkageError: loader constraint violation: when resolving interface method "javax. ...

  8. 【已解决】报异常:java.lang.ArithmeticException: / by zero

    异常名称如下:java.lang.ArithmeticException: / by zero 原因:当我们定义的被除数为整形时候(short  int  long)会抛出此异常,被除数为整形时不可以 ...

  9. 异常java.lang.IllegalArgumentException:attempt to create delete event with null entity

    异常java.lang.IllegalArgumentException:attempt to create delete event with null entity解决:路径问题,前台jsp和ja ...

随机推荐

  1. DLL对应的导入库一定会生成的

    测试代码: #pragma once #define TESTDEPEND_EXPORTS #ifdef TESTDEPEND_EXPORTS #define TESTDEPEND_API __dec ...

  2. springBoot中的邮件发送

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. UML-用例关联

    1.用例关联:就是各个用例之间的关系,分3种关系分别是:包含关系.扩展关系.泛化关系. 2.包含关系 1).示例 2).使用场景 A.用例在其他用例中重复使用 B.用例非常复杂冗长,将其分解为子单元便 ...

  4. Mybatis实现if trim(四)

    1. 准备 请先完成Mybatis实现增删改查(二)和Mybatis实现条件查询(三)的基本内容 2. 关于多条件查询的疑问 在Mybatis实现条件查询(三)中我们实现了多条件(商品编码.商品名称. ...

  5. POJ 1850:Code 组合数学

    Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8710   Accepted: 4141 Description ...

  6. 操作uwsgi命令

    uwsgi -i 你的目录/uwsgi.ini & 后台开启uwsgi pkill -f uwsgi 重启uwsgi

  7. error_reporting() 设置 PHP 的报错级别并返回当前级别

    error_reporting() 设置 PHP 的报错级别并返回当前级别. 语法 error_reporting(report_level) 如果参数 level 未指定,当前报错级别将被返回.下面 ...

  8. 专业程序设计part1

    7 专业程序设计 (有多少人在大学里学到了自己真正喜欢的专业??并在此专业上获得了升华??)i== 软件图标下载网址:easyicon 01thu 4邻域 8邻域 D邻域 是啥 像素间的连通性,是转为 ...

  9. Unity中的事件监听

    Unity3D的uGUI系统的将UI可能触发的事件分为12个类型,即EventTriggerType枚举的12个值.如下图所示: 先以PointerClick为例.这个是用于某点点击事件.其他事件都可 ...

  10. ZJNU 1422 - 碰撞的小球

    完全弹性碰撞可以视作互相穿过 所以直接考虑只有单个小球的时候,从板子上滑下所需要的时间即可 最后以30000为界分开流读入与缓冲区优化的io方法 //Case4用缓冲区io优化会WA??? /* Wr ...