最近在处理支付相关的需求,涉及到金额的问题,采用传统的基本数据类型处理会存在误差,因此采用BigDecimal对象进行处理。

一、构造BigDecimal对象的方式

BigDecimal(int)       创建一个具有参数所指定整数值的对象。

BigDecimal(double) 创建一个具有参数所指定双精度值的对象。

BigDecimal(long)    创建一个具有参数所指定长整数值的对象。

BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。

注:建议采用BigDecimal(String)进行构造创建BigDecimal对象。

二、BigDecimal对象的加、减、乘、除操作

add(BigDecimal)        BigDecimal对象中的值相加,然后返回这个对象。

subtract(BigDecimal) BigDecimal对象中的值相减,然后返回这个对象。

multiply(BigDecimal)  BigDecimal对象中的值相乘,然后返回这个对象。

divide(BigDecimal)     BigDecimal对象中的值相除,然后返回这个对象。

三、BigDecimal保留小数点问题

1、ROUND_DOWN 

Rounding mode to round towards zero.

向零方向舍入

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 5
2.5 2
1.6 1
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5

2、ROUND_UP 

Rounding mode to round away from zero.

向远离0的方向舍入

示例:

输入数字 使用 UP 舍入模式将输入数字舍入为一位数
5.5 6
2.5 3
1.6 2
1.1 2
1.0 1
-1.0 -1
-1.1 -2
-1.6 -2
-2.5 -3
-5.5 -6

3、ROUND_CEILING 

Rounding mode to round towards positive infinity.

向正无穷方向舍入

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 6
2.5 3
1.6 2
1.1 2
1.0 1
-1.0 -1
-1.1 -1
-1.6 -1
-2.5 -2
-5.5 -5

4、ROUND_FLOOR 

Rounding mode to round towards negative infinity.

向负无穷方向舍入

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 5
2.5 2
1.6 1
1.1 1
1.0 1
-1.0 -1
-1.1 -2
-1.6 -2
-2.5 -3
-5.5 -6

5、ROUND_HALF_DOWN (相当于五舍六入

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.

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

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 5
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -5

6、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

解释:如果舍弃部分左边的数字为奇数,则舍入行为同 RoundingMode.HALF_UP;如果为偶数,则舍入行为同RoundingMode.HALF_DOWN。注意,在重复进行一系列计算时,根据统计学,此舍入模式可以在统计上将累加错误减到最小。此舍入模式也称为“银行家舍入法”,主要在美国使用。此舍入模式类似于 Java 中对float 和double 算法使用的舍入策略。

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 6
2.5 2
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -2
-5.5 -6

7、ROUND_HALF_UP (相当于四舍五入

Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.

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

示例:

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 6
2.5 3
1.6 2
1.1 1
1.0 1
-1.0 -1
-1.1 -1
-1.6 -2
-2.5 -3
-5.5 -6

8、ROUND_UNNECESSARY 

Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.

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

解释:计算结果是精确的,不需要舍入,否则抛出 ArithmeticException。

输入数字 使用 DOWN 舍入模式将输入数字舍入为一位数
5.5 抛出 ArithmeticException
2.5 抛出 ArithmeticException
1.6 抛出 ArithmeticException
1.1 抛出 ArithmeticException
1.0 1
-1.0 -1
-1.1 抛出 ArithmeticException
-1.6 抛出 ArithmeticException
-2.5 抛出 ArithmeticException
-5.5 抛出 ArithmeticException

感谢道友的博客:https://my.oschina.net/sunchp/blog/670909 ,https://www.cnblogs.com/liqforstudy/p/5652517.html

BigDecimal保留小数处理的更多相关文章

  1. BigDecimal保留小数

    public class test1_format { public static void main(String[] args) { BigDecimal decimal = new BigDec ...

  2. java 保留小数点后N位数(若干位),几种实现的方式总结

    import java.math.BigDecimal;import java.text.DecimalFormat;import java.text.NumberFormat;/** * java ...

  3. java保留小数-抄网上的

    摘抄别人的JAVA中保留小数点后若干位数的几种方法  2009-12-17 11:46:18|  分类: 编程小发现 |  标签: |举报 |字号大中小 订阅 第一种:java.text.Decima ...

  4. Java指定保留小数位数的方法

    package com.qiyuan.util; import java.math.BigDecimal; import java.math.RoundingMode; import java.tex ...

  5. Java四舍五入 保留小数

    java 四舍五入保留小数   // 方式一: double f = 3.1516; BigDecimal b = new BigDecimal(f); double f1 = b.setScale( ...

  6. 对数值数据的格式化处理(保留小数点后N位)

    项目中有时会遇到对数值部分进行保留操作,列如保留小数点后2位,所有的数据都按这种格式处理, //保留小数点后2位,都按这种格式处理,没有补0 DecimalFormat df = new Decima ...

  7. [Java]对double变量进行四舍五入,并保留小数点后位数

    1.功能 将double类型变量进行四舍五入,并保留小数点后位数 2.代码 import java.math.BigDecimal; import java.math.RoundingMode; im ...

  8. java 保留小数点后指定位数四种方法

    1 package com.itheima_01; 2 3 import java.math.BigDecimal; 4 import java.text.DecimalFormat; 5 impor ...

  9. 格式化 float 类型,保留小数点后1位

    """  练习 :   小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,   并用字符串格式化显示出'xx.x%',只保留小数点后1位: &qu ...

随机推荐

  1. 《剑指offer》面试题15 链表中的倒数第k个节点 Java版

    书中方法:用两个节点一次遍历求得倒数第k个节点.注意头节点为空,k<=0,k大于节点个数的情况. public ListNode find(ListNode head, int k){ if(h ...

  2. Windows 中下载 Android Q 源码

      1.  安装软件 1.1.  安装 git A.git官网下载:https://git-scm.com/downloads/ 安装git到如下路径 C:/Program Files/Git B.图 ...

  3. C# 中File和FileStream的用法

    原文:https://blog.csdn.net/qq_41209575/article/details/89178020 1.首先先介绍File类和FileStream文件流 1.1  File类, ...

  4. Floyd(弗洛伊德)算法(C语言)

    转载:https://blog.csdn.net/qq_35644234/article/details/60875818 Floyd算法的介绍 算法的特点 弗洛伊德算法是解决任意两点间的最短路径的一 ...

  5. python面向对象--类的内置函数

    #isinstance(obj,cls)判断obj是否是类cls的实例 #issubclass(cls,cls1)判断cls是否是cls1的子类或派生类 class Foo: pass class B ...

  6. HDU-3810 超大容量01背包

    题意:有n堆野兽,每堆野兽屠杀完完需要花费ti时间,可以增加金钱gi,敌法师有瞬移技能,可以从某堆野兽移到另一堆野兽,题目有给定从哪堆可以移到哪堆.最后问在满足打的金钱多余m的情况下的最少时间.数据范 ...

  7. [python 学习] lambda

    #!/usr/bin/python # -*- encoding:utf-8 -*- # lambda parameter_list: expression f = lambda x,y: x + y ...

  8. thinkphp数据库连接

    https://www.kancloud.cn/manual/thinkphp5/118059 一.配置文件定义 常用的配置方式是在应用目录或者模块目录下面的database.php中添加下面的配置参 ...

  9. 在PHPstorm上安装thinkPHP

    >环境:ubuntu php7.2 phpstorm https://blog.csdn.net/roukmanx/article/details/85646174 https://www.ka ...

  10. python 日期封装

    import time import datetime import locale class TimeUtil: def __init__(self, curtime=None): self.cur ...