淺談BigDecimal類在電子商務中至關重要的地位!

來源:互聯網
上載者:User

一提到Java裡面的商業計算,我們都知道不能用float和double,因為他們無法進行精確計算。但是Java的設計者給編程人員提供了一個很有用的類BigDecimal,他可以完善float和double類無法進行精確計算的缺憾。BigDecimal類位於java.maths類包下。首先我們來看下如何構造一個BigDecimal對象。它的建構函式很多,我挑最常用的兩個來示範一下:一個就是BigDecimal(double val),另一個就是BigDecimal(String str)。這兩個看上去沒什麼太大區別,但是正像API描述中說的那樣:

view plaincopy to clipboardprint?
/*The results of this constructor can be somewhat unpredictable. One might assume that  
new BigDecimal(.1) is exactly equal to .1, but it is actually equal  
to .1000000000000000055511151231257827021181583404541015625. This is so because .1  
cannot be represented exactly as a double (or, for that matter, as a binary fraction  
of any finite length). Thus, the long value that is being passed in to the constructor  
is not exactly equal to .1, appearances nonwithstanding. 
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal 
(".1") is exactly equal to .1, as one would expect. Therefore, it is generally  
recommended that the (String) constructor be used in preference to this one.*/ 
/*The results of this constructor can be somewhat unpredictable. One might assume that
new BigDecimal(.1) is exactly equal to .1, but it is actually equal
to .1000000000000000055511151231257827021181583404541015625. This is so because .1
cannot be represented exactly as a double (or, for that matter, as a binary fraction
of any finite length). Thus, the long value that is being passed in to the constructor
is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal
(".1") is exactly equal to .1, as one would expect. Therefore, it is generally
recommended that the (String) constructor be used in preference to this one.*/

也就是說利用double作為參數的建構函式,無法精確構造一個BigDecimal對象,需要自己指定一個內容相關的環境,也就是指定精確位。而利用String對象作為參數傳入的建構函式能精確的構造出一個BigDecimal對象。請看下面的代碼:

view plaincopy to clipboardprint?
import java.math.*;  
public class TestBigDecimal {  
    public static void main(String args[]){  
        BigDecimal bd = new BigDecimal("10.123");  
        BigDecimal bd1 = new BigDecimal(10.123);  
        System.out.println(bd +"/n"+ bd1);  
    }  

import java.math.*;

public class TestBigDecimal {
    public static void main(String args[]){
        BigDecimal bd = new BigDecimal("10.123");
        BigDecimal bd1 = new BigDecimal(10.123);

        System.out.println(bd +"/n"+ bd1);

    }
}

輸出:

view plaincopy to clipboardprint?
10.123 
10.1229999999999993320898283855058252811431884765625 
10.123
10.1229999999999993320898283855058252811431884765625

所以我們在選擇建構函式時,要看具體需求而定。

另外,很多人會問到怎麼將基本類型,如int,float,double,long,和BigDecimal對象相互轉換。很簡單:

基本類型通過建構函式轉換成對應的BigDecimal對象,而BigDecimal類提供了諸如intValue(), floatValue(), doubleValue(), longValue()方法來將BigDecimal對象轉換成對應的值。

關於BigDecimal是如何計算的,我以論壇中一個人的提問文章為例,來簡單的寫出BigDecimal的運算方法。題目是:李白無事街上走,提壺去買酒。遇店加一倍,見花喝一鬥,五遇花和店,喝光壺中酒,試問李白壺中原有多少鬥酒?

這道題應該是從後往前推,並且要逆運算,最後得出原有酒的體積。

view plaincopy to clipboardprint?
import java.math.*;  
public class Libai {  
    public static void main(String args[]){  
        BigDecimal volumn = new BigDecimal("0");  
        for (int i=0; i<5;  i++){             
            volumn = volumn.add(new BigDecimal("1"));  
            volumn = volumn.divide(new BigDecimal("2"));  
        }  
        System.out.print(volumn);  
    }  

import java.math.*;

public class Libai {
    public static void main(String args[]){
        BigDecimal volumn = new BigDecimal("0");
        for (int i=0; i<5;  i++){          
            volumn = volumn.add(new BigDecimal("1"));
            volumn = volumn.divide(new BigDecimal("2"));
        }
        System.out.print(volumn);
    }
}

結果:

view plaincopy to clipboardprint?
0.96875 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.