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種舍入方式值得掌握。java
一、ROUND_UPspring
舍入遠離零的舍入模式。springboot
在丟棄非零部分以前始終增長數字(始終對非零捨棄部分前面的數字加1)。mybatis
注意,此舍入模式始終不會減小計算值的大小。post
二、ROUND_DOWNspa
接近零的舍入模式。code
在丟棄某部分以前始終不增長數字(從不對捨棄部分前面的數字加1,即截短)。blog
注意,此舍入模式始終不會增長計算值的大小。ip
三、ROUND_CEILINGci
接近正無窮大的舍入模式。
若是 BigDecimal 爲正,則舍入行爲與 ROUND_UP 相同;
若是爲負,則舍入行爲與 ROUND_DOWN 相同。
注意,此舍入模式始終不會減小計算值。
四、ROUND_FLOOR
接近負無窮大的舍入模式。
若是 BigDecimal 爲正,則舍入行爲與 ROUND_DOWN 相同;
若是爲負,則舍入行爲與 ROUND_UP 相同。
注意,此舍入模式始終不會增長計算值。
五、ROUND_HALF_UP
向「最接近的」數字舍入,若是與兩個相鄰數字的距離相等,則爲向上舍入的舍入模式。
若是捨棄部分 >= 0.5,則舍入行爲與 ROUND_UP 相同;不然舍入行爲與 ROUND_DOWN 相同。
注意,這是咱們大多數人在小學時就學過的舍入模式(四捨五入)。
六、ROUND_HALF_DOWN
向「最接近的」數字舍入,若是與兩個相鄰數字的距離相等,則爲上舍入的舍入模式。
若是捨棄部分 > 0.5,則舍入行爲與 ROUND_UP 相同;不然舍入行爲與 ROUND_DOWN 相同(五舍六入)。
七、ROUND_HALF_EVEN 銀行家舍入法
向「最接近的」數字舍入,若是與兩個相鄰數字的距離相等,則向相鄰的偶數舍入。
若是捨棄部分左邊的數字爲奇數,則舍入行爲與 ROUND_HALF_UP 相同;
若是爲偶數,則舍入行爲與 ROUND_HALF_DOWN 相同。
注意,在重複進行一系列計算時,此舍入模式能夠將累加錯誤減到最小。
此舍入模式也稱爲「銀行家舍入法」,主要在美國使用。四捨六入,五分兩種狀況。
若是前一位爲奇數,則入位,不然捨去。
如下例子爲保留小數點1位,那麼這種舍入方式下的結果。
1.15>1.2 1.25>1.2
八、ROUND_UNNECESSARY
斷言請求的操做具備精確的結果,所以不須要舍入。
若是對得到精確結果的操做指定此舍入模式,則拋出ArithmeticException。
經常使用取值四捨五入(ROUND_HALF_UP) ;
由於咱們是屬於互聯網金融行業,全部在進行計算的時候儘可能使用ROUND_HALF_EVEN 銀行家舍入法