BigDecimal保留小數處理


 

最近在處理支付相關的需求,涉及到金額的問題,采用傳統的基本數據類型處理會存在誤差,因此采用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM