转载地址: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. 关于mysql/apache/nginx的配置文件配置

    2015.5.12 mysql中可以通过函数查看用户列表/查看当前用户以及删除用户,方法百度之,忘了 /etc/php5/fpm/php.ini memory_limit修改之前为128M apach ...

  2. 使用mysql的注意事项

    1,文件导入:LOAD DATA INFILE '/tmp/pet.txt' INTO TABLE pet FIELDS TERMINATED BY ',' LINES TERMINATED BY ' ...

  3. 反编译查看printf()的方法

    源代码: package test2; public class ExplorationJDKSource { /** * @param args */ public static void main ...

  4. nexus3安装 - CentOS7环境

    nexus3安装 - CentOS7环境 使用nexus3管理docker镜像,配合rancher进行部署. 建资料卷 资料卷默认地址:/var/lib/docker/volumes/资料卷名/_da ...

  5. vscode 集成git bash, mingw, mintty 的terminal

    设置 右上角打开json文件的设置 输入以下代码: "terminal.external.windowsExec": "D:\\Program Files\\Git\\b ...

  6. hadoop cmd

    一.hadoop文件操作 1.Ls hadoop fs -ls / 2.Put hadoop fs -put xx /path 3.Mkdir hadoop fs -mkdir 4.要从HDFS中删除 ...

  7. POJ 1847:Tram

    Tram Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11771   Accepted: 4301 Description ...

  8. 经理人和app开发者大打出手,说明这个市场已经畸形变态?

    日前,一件民生事件在网络上广为流传,成为人们热议的话题:中国平安的产品经理向app开发者提了一个需求,要求用户app的主题颜色能根据手机外壳自动调整,可能是开发人员觉得这个要求太不合理,而且感到十分绝 ...

  9. CMake命令之export

    CMake中与export()相关的命令 (注:红色字体是标题,粉色是需要特别需要注意的地方) 总的来说,export()命令想要做的事情可以用一句话概括:Export targets from th ...

  10. 标准库模块——shutil模块

    shutil.rmtee 删除目录及以内的所有文件. import shutil shutil.rmtree(r'D:\python\222') #包括222在内的所有文件全部删除.