package com.qjx.springbootmybatis.Test; import java.math.BigDecimal; /** * @ClassName:MTest * @Author:QinJinxiang * @Date:2018/6/27 17:56 * @Description:TODO **/ public class MTest { public static void main(String[] args) { System.out.println("---------------"); ROUND_UP(); System.out.println("---------------"); ROUND_DOWN(); System.out.println("---------------"); ROUND_CEILING(); System.out.println("---------------"); ROUND_FLOOR(); System.out.println("---------------"); ROUND_HALF_UP(); System.out.println("---------------"); ROUND_HALF_DOWN(); System.out.println("---------------"); ROUND_HALF_EVEN(); System.out.println("---------------"); ROUND_UNNECESSARY(); System.out.println("---------------"); } private static void ROUND_UP(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_UP); System.out.println("ROUND_UP 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_UP); System.out.println("ROUND_UP 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_UP); System.out.println("ROUND_UP 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_UP); System.out.println("ROUND_UP -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_UP); System.out.println("ROUND_UP -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_UP); System.out.println("ROUND_UP -12.4444 = "+f); } private static void ROUND_DOWN(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_DOWN); System.out.println("ROUND_DOWN 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_DOWN); System.out.println("ROUND_DOWN 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_DOWN); System.out.println("ROUND_DOWN 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_DOWN); System.out.println("ROUND_DOWN -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_DOWN); System.out.println("ROUND_DOWN -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_DOWN); System.out.println("ROUND_DOWN -12.4444 = "+f); } private static void ROUND_CEILING(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_CEILING); System.out.println("ROUND_CEILING 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_CEILING); System.out.println("ROUND_CEILING 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_CEILING); System.out.println("ROUND_CEILING 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_CEILING); System.out.println("ROUND_CEILING -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_CEILING); System.out.println("ROUND_CEILING -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_CEILING); System.out.println("ROUND_CEILING -12.4444 = "+f); } private static void ROUND_FLOOR(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_FLOOR); System.out.println("ROUND_FLOOR 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_FLOOR); System.out.println("ROUND_FLOOR 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_FLOOR); System.out.println("ROUND_FLOOR 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_FLOOR); System.out.println("ROUND_FLOOR -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_FLOOR); System.out.println("ROUND_FLOOR -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_FLOOR); System.out.println("ROUND_FLOOR -12.4444 = "+f); } private static void ROUND_HALF_UP(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("ROUND_HALF_UP 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("ROUND_HALF_UP 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("ROUND_HALF_UP 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("ROUND_HALF_UP -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("ROUND_HALF_UP -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println("ROUND_HALF_UP -12.4444 = "+f); } private static void ROUND_HALF_DOWN(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_HALF_DOWN); System.out.println("ROUND_HALF_DOWN 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_HALF_DOWN); System.out.println("ROUND_HALF_DOWN 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_HALF_DOWN); System.out.println("ROUND_HALF_DOWN 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_HALF_DOWN); System.out.println("ROUND_HALF_DOWN -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_HALF_DOWN); System.out.println("ROUND_HALF_DOWN -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_HALF_DOWN); System.out.println("ROUND_HALF_DOWN -12.4444 = "+f); } private static void ROUND_HALF_EVEN(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_HALF_EVEN); System.out.println("ROUND_HALF_EVEN 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_HALF_EVEN); System.out.println("ROUND_HALF_EVEN 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_HALF_EVEN); System.out.println("ROUND_HALF_EVEN 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_HALF_EVEN); System.out.println("ROUND_HALF_EVEN -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_HALF_EVEN); System.out.println("ROUND_HALF_EVEN -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_HALF_EVEN); System.out.println("ROUND_HALF_EVEN -12.4444 = "+f); } private static void ROUND_UNNECESSARY(){ BigDecimal a =new BigDecimal(String.valueOf(12.5555)).setScale(2,BigDecimal.ROUND_UNNECESSARY); System.out.println("ROUND_UNNECESSARY 12.5555 = "+a); BigDecimal b =new BigDecimal(String.valueOf(12.6666)).setScale(2,BigDecimal.ROUND_UNNECESSARY); System.out.println("ROUND_UNNECESSARY 12.6666 = "+b); BigDecimal c =new BigDecimal(String.valueOf(12.4444)).setScale(2,BigDecimal.ROUND_UNNECESSARY); System.out.println("ROUND_UNNECESSARY 12.4444 = "+c); BigDecimal d =new BigDecimal(String.valueOf(-12.5555)).setScale(2,BigDecimal.ROUND_UNNECESSARY); System.out.println("ROUND_UNNECESSARY -12.5555 = "+d); BigDecimal e =new BigDecimal(String.valueOf(-12.6666)).setScale(2,BigDecimal.ROUND_UNNECESSARY); System.out.println("ROUND_UNNECESSARY -12.6666 = "+e); BigDecimal f =new BigDecimal(String.valueOf(-12.4444)).setScale(2,BigDecimal.ROUND_UNNECESSARY); System.out.println("ROUND_UNNECESSARY -12.4444 = "+f); } }
运行结果: --------------- ROUND_UP 12.5555 = 12.56 ROUND_UP 12.6666 = 12.67 ROUND_UP 12.4444 = 12.45 ROUND_UP -12.5555 = -12.56 ROUND_UP -12.6666 = -12.67 ROUND_UP -12.4444 = -12.45 --------------- ROUND_DOWN 12.5555 = 12.55 ROUND_DOWN 12.6666 = 12.66 ROUND_DOWN 12.4444 = 12.44 ROUND_DOWN -12.5555 = -12.55 ROUND_DOWN -12.6666 = -12.66 ROUND_DOWN -12.4444 = -12.44 --------------- ROUND_CEILING 12.5555 = 12.56 ROUND_CEILING 12.6666 = 12.67 ROUND_CEILING 12.4444 = 12.45 ROUND_CEILING -12.5555 = -12.55 ROUND_CEILING -12.6666 = -12.66 ROUND_CEILING -12.4444 = -12.44 --------------- ROUND_FLOOR 12.5555 = 12.55 ROUND_FLOOR 12.6666 = 12.66 ROUND_FLOOR 12.4444 = 12.44 ROUND_FLOOR -12.5555 = -12.56 ROUND_FLOOR -12.6666 = -12.67 ROUND_FLOOR -12.4444 = -12.45 --------------- ROUND_HALF_UP 12.5555 = 12.56 ROUND_HALF_UP 12.6666 = 12.67 ROUND_HALF_UP 12.4444 = 12.44 ROUND_HALF_UP -12.5555 = -12.56 ROUND_HALF_UP -12.6666 = -12.67 ROUND_HALF_UP -12.4444 = -12.44 --------------- ROUND_HALF_DOWN 12.5555 = 12.56 ROUND_HALF_DOWN 12.6666 = 12.67 ROUND_HALF_DOWN 12.4444 = 12.44 ROUND_HALF_DOWN -12.5555 = -12.56 ROUND_HALF_DOWN -12.6666 = -12.67 ROUND_HALF_DOWN -12.4444 = -12.44 --------------- ROUND_HALF_EVEN 12.5555 = 12.56 ROUND_HALF_EVEN 12.6666 = 12.67 ROUND_HALF_EVEN 12.4444 = 12.44 ROUND_HALF_EVEN -12.5555 = -12.56 ROUND_HALF_EVEN -12.6666 = -12.67 ROUND_HALF_EVEN -12.4444 = -12.44 ---------------
在银行、帐户、计费等领域,BigDecimal提供了精确的数值计算。其中8种舍入方式值得掌握。
1、ROUND_UP
舍入远离零的舍入模式。
在丢弃非零部分之前始终增加数字(始终对非零舍弃部分前面的数字加1)。
注意,此舍入模式始终不会减少计算值的大小。
2、ROUND_DOWN
接近零的舍入模式。
在丢弃某部分之前始终不增加数字(从不对舍弃部分前面的数字加1,即截短)。
注意,此舍入模式始终不会增加计算值的大小。
3、ROUND_CEILING
接近正无穷大的舍入模式。
如果 BigDecimal 为正,则舍入行为与 ROUND_UP 相同;
如果为负,则舍入行为与 ROUND_DOWN 相同。
注意,此舍入模式始终不会减少计算值。
4、ROUND_FLOOR
接近负无穷大的舍入模式。
如果 BigDecimal 为正,则舍入行为与 ROUND_DOWN 相同;
如果为负,则舍入行为与 ROUND_UP 相同。
注意,此舍入模式始终不会增加计算值。
5、ROUND_HALF_UP
向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为向上舍入的舍入模式。
如果舍弃部分 >= 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同。
注意,这是我们大多数人在小学时就学过的舍入模式(四舍五入)。
6、ROUND_HALF_DOWN
向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则为上舍入的舍入模式。
如果舍弃部分 > 0.5,则舍入行为与 ROUND_UP 相同;否则舍入行为与 ROUND_DOWN 相同(五舍六入)。
7、ROUND_HALF_EVEN 银行家舍入法
向“最接近的”数字舍入,如果与两个相邻数字的距离相等,则向相邻的偶数舍入。
如果舍弃部分左边的数字为奇数,则舍入行为与 ROUND_HALF_UP 相同;
如果为偶数,则舍入行为与 ROUND_HALF_DOWN 相同。
注意,在重复进行一系列计算时,此舍入模式可以将累加错误减到最小。
此舍入模式也称为“银行家舍入法”,主要在美国使用。四舍六入,五分两种情况。
如果前一位为奇数,则入位,否则舍去。
以下例子为保留小数点1位,那么这种舍入方式下的结果。
1.15>1.2 1.25>1.2
8、ROUND_UNNECESSARY
断言请求的操作具有精确的结果,因此不需要舍入。
如果对获得精确结果的操作指定此舍入模式,则抛出ArithmeticException。
常用取值四舍五入(ROUND_HALF_UP) ;
因为我们是属于互联网金融行业,所有在进行计算的时候尽量使用ROUND_HALF_EVEN 银行家舍入法