Blueimp 論壇首頁
  首頁  | 討論區  | 最新話題  | 搜尋  | XML  |  登入

此話題中所有文章數: 3 [ 話題狀態: 一般 ]
上一話題 此文章已經觀看 35 次 而且有 2 篇回應 下一話題
會員大頭照
男寶寶 Moonnight 《護衛隊長》
文章: 716
v3.8.8

java.math.BigDecimal 之中 setScale() 的一些測試

java.math.BigDecimal 之中 setScale() 的一些測試


1.ROUND_UP
 Rounding mode to round away from zero.
 無條件進入,取絕對值後相鄰數值較大的那個。(取不接近 0 的那一個)
2.ROUND_DOWN
 無條件捨去,取絕對值後相鄰數值較小的那個。(取接近 0 那一個)
 Rounding mode to round towards zero
3.ROUND_CEILING
 正數:無條件進位
 負數:無條件捨去
 趨向正無限大
 Rounding mode to round towards positive infinity
4.ROUND_FLOOR
 正數:無條件捨去
 負數:無條件進位
 趨向負無限大
 Rounding mode to round towards negative infinity
5.ROUND_HALF_UP
 Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
 四捨五入,當在中間值時,比方 -1.5 ,將往非0的方向,成為 -2
6.ROUND_HALF_DOWN
 Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
 四捨五入,當在中間值時,比方 -1.5 ,將往 0 的方向,成為 -1
7.ROUND_HALF_EVEN
 Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor
 四捨五入,當在中間值時,比方 -1.5 ,將往偶數的方向,成為 -2
8.ROUND_UNNECESSARY
 Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
 不知道怎麼測試...

測試結果:
*********** ROUND_UP: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.11
: -34.101 --> -34.11
: 34.105 --> 34.11
: -34.105 --> -34.11
: 34.106 --> 34.11
: -34.106 --> -34.11
*********** ROUND_DOWN: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.10
: -34.101 --> -34.10
: 34.105 --> 34.10
: -34.105 --> -34.10
: 34.106 --> 34.10
: -34.106 --> -34.10
*********** ROUND_FLOOR: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.10
: -34.101 --> -34.11
: 34.105 --> 34.10
: -34.105 --> -34.11
: 34.106 --> 34.10
: -34.106 --> -34.11
*********** ROUND_CEILING: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.11
: -34.101 --> -34.10
: 34.105 --> 34.11
: -34.105 --> -34.10
: 34.106 --> 34.11
: -34.106 --> -34.10
*********** ROUND_HALF_UP: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.10
: -34.101 --> -34.10
: 34.105 --> 34.11
: -34.105 --> -34.11
: 34.106 --> 34.11
: -34.106 --> -34.11
*********** ROUND_HALF_DOWN: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.10
: -34.101 --> -34.10
: 34.105 --> 34.10
: -34.105 --> -34.10
: 34.106 --> 34.11
: -34.106 --> -34.11
*********** ROUND_HALF_EVEN: ***********
: 34.00 --> 34.00
: -34.00 --> -34.00
: 34.101 --> 34.10
: -34.101 --> -34.10
: 34.105 --> 34.10
: -34.105 --> -34.10
: 34.106 --> 34.11
: -34.106 --> -34.11


[2005/7/5 上午 11:27:53]   [返迴此篇文章頂端 ]  回到頂端 
會員大頭照
男寶寶 Moonnight 《護衛隊長》
文章: 716
v3.8.8

Re: java.math.BigDecimal 之中 setScale() 的一些測試

http://java.sun.com/j2se/1.5.0/docs/api/java/math/RoundingMode.html

-- url from jieh post
----------------------------------------
[編輯文章 1 次, 最後修改: Moonnight 於 2005/7/5 上午 11:32:13]
[2005/7/5 上午 11:29:00]   [返迴此篇文章頂端 ]  回到頂端 
會員大頭照
男寶寶 Moonnight 《護衛隊長》
文章: 716
v3.8.8

Re: java.math.BigDecimal 之中 setScale() 的一些測試

為何要貼這一篇及做這個測試呢?
起源是來自於 float 這個內容會浮來浮去的浮點數...

先請大家看一下擷取自 javaworld 的討論 :

(...前略)
這個問題就是 IEEE754 浮點數運算( IEEE 754 floating-point arithmetic) , 這可在 Java 語言中找到, Java 語言並不使用小數點(或十進位)去表現數字,相反的它採用(二進位)分數和指數,要實際了解以上所說是什麼意思,我們考慮試著寫出數個小數以分數表示的式子
0.5 = 1/2
0.75 = 1/2 + 1/4
0.875 = 1/2 + 1/4 + 1/8
0.1 = 1/16 + 1/32 + 1/256 + 1/512 +1/4096 + 1/8192 + ...
最後這一個級數是無窮止境的繼續下去,這意味著 0.1 不能準確的以浮點數型態表現,這無窮級數必須在某一點切斷, 做一些捨去或進位的動作...等等, 目的要以64位元double型態表現,而這將會導致一些誤差 (...後略)


引用資料來源 - javaworld


================

使用 float 表示小數常常會有不精準的問題,因而考慮使用 java.math.BigDecimal 去取代 float 。

以加總合計當範例,請各位參考底下簡單範例:


private void test010() {
BigDecimal[] bd = new BigDecimal[5];
bd[0] = new BigDecimal("323.309");
bd[1] = new BigDecimal("11221.4");
bd[2] = new BigDecimal("2733060.0");
bd[3] = new BigDecimal("311080.0");
bd[4] = new BigDecimal("311080.0");
BigDecimal total = new BigDecimal("0");

for (int i = 0, length = bd.length; i < length; i++) {
total = total.add(bd);
}

System.out.println(total.toString()); // 一般顯示
System.out.println("ROUND_HALF_UP"+total.setScale(2,total.ROUND_HALF_UP).toString()); // 四捨五入到小數後第二位
}
----------------------------------------
[編輯文章 1 次, 最後修改: Moonnight 於 2005/7/5 上午 11:31:18]
[2005/7/5 上午 11:30:02]   [返迴此篇文章頂端 ]  回到頂端