<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    磨刀不誤砍柴工

    合抱之木,生于毫末;九層之臺,起于累土;千里之行,始于足下。

       ::  ::  ::  :: 管理

    Java 浮點數精確計算 BigDecimal的用法

    java.math.BigDecimal的用法?

    Java 浮點數 精確計算  

    如果我們編譯運行下面這個程序會看到什么?

    public class Test{

        public static void main(String args[]){

            System.out.println(0.05+0.01);

            System.out.println(1.0-0.42);

            System.out.println(4.015*100);

            System.out.println(123.3/100);

        }

    };

    你沒有看錯!結果確實是

    0.060000000000000005

    0.5800000000000001

    401.49999999999994

    1.2329999999999999

    Java中的簡單浮點數類型float和double不能夠進行運算。不光是Java,在其它很多編程語言中也有這樣的問題。在大多數情況下,計算的結果是準確的,但是多試幾次(可以做一個循環)就可以試出類似上面的錯誤。現在終于理解為什么要有BCD碼了。

    這個問題相當嚴重,如果你有9.999999999999元,你的計算機是不會認為你可以購買10元的商品的。

    在有的編程語言中提供了專門的貨幣類型來處理這種情況,但是Java沒有。現在讓我們看看如何解決這個問題。

    四舍五入

    我們的第一個反應是做四舍五入。Math類中的round方法不能設置保留幾位小數,我們只能象這樣(保留兩位):

    public double round(double value){

        return Math.round(value*100)/100.0;

    }

    非常不幸,上面的代碼并不能正常工作,給這個方法傳入4.015它將返回4.01而不是4.02,如我們在上面看到的

    4.015*100=401.49999999999994

    因此如果我們要做到精確的四舍五入,不能利用簡單類型做任何運算

    java.text.DecimalFormat也不能解決這個問題:

    System.out.println(new java.text.DecimalFormat("0.00").format(4.025));

    輸出是4.02

    BigDecimal

    在《Effective Java》這本書中也提到這個原則,float和double只能用來做科學計算或者是工程計算,在商業計算中我們要用 java.math.BigDecimal。BigDecimal一共有4個夠造方法,我們不關心用BigInteger來夠造的那兩個,那么還有兩個,它們是:

    BigDecimal(double val)

              Translates a double into a BigDecimal.

    BigDecimal(String val)

              Translates the String repre sentation of a BigDecimal into a BigDecimal.

    上面的API簡要描述相當的明確,而且通常情況下,上面的那一個使用起來要方便一些。我們可能想都不想就用上了,會有什么問題呢?等到出了問題的時候,才發現上面哪個夠造方法的詳細說明中有這么一段:

    Note: 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.

    原來我們如果需要精確計算,非要用String來夠造BigDecimal不可!在《Effective Java》一書中的例子是用String來夠造BigDecimal的,但是書上卻沒有強調這一點,這也許是一個小小的失誤吧。

    解決方案

    現在我們已經可以解決這個問題了,原則是使用BigDecimal并且一定要用String來夠造。

    但是想像一下吧,如果我們要做一個加法運算,需要先將兩個浮點數轉為String,然后夠造成BigDecimal,在其中一個上調用add方法,傳入另一個作為參數,然后把運算的結果(BigDecimal)再轉換為浮點數。你能夠忍受這么煩瑣的過程嗎?下面我們提供一個工具類Arith來簡化操作。它提供以下靜態方法,包括加減乘除和四舍五入:

    public static double add(double v1,double v2)

    public static double sub(double v1,double v2)

    public static double mul(double v1,double v2)

    public static double div(double v1,double v2)

    public static double div(double v1,double v2,int scale)

    public static double round(double v,int scale)

    附錄

     


     

    BigDecimal舍入模式(Rounding mode)
    關鍵字: bigdecimal

    ROUND_CEILING
    Rounding mode to round towards positive infinity.
    向正無窮方向舍入
    ROUND_DOWN
    Rounding mode to round towards zero.
    向零方向舍入
    ROUND_FLOOR
    Rounding mode to round towards negative infinity.
    向負無窮方向舍入
    ROUND_HALF_DOWN
    Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down.
    向(距離)最近的一邊舍入,除非兩邊(的距離)是相等,如果是這樣,向下舍入, 例如1.55 保留一位小數結果為1.5
    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
    ROUND_HALF_UP
    Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up.
    向(距離)最近的一邊舍入,除非兩邊(的距離)是相等,如果是這樣,向上舍入, 1.55保留一位小數結果為1.6
    ROUND_UNNECESSARY
    Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
    計算結果是精確的,不需要舍入模式
    ROUND_UP
    Rounding mode to round away from zero.
    向遠離0的方向舍入
    例子

    Java代碼

    1. BigDecimal n3  = new BigDecimal(1.2345678901234567890E+9);  
    2. System.out.println(n3) 
    BigDecimal n3  = new BigDecimal(1.2345678901234567890E+9);
    System.out.println(n3)

    這樣輸出的結果是:1234567890.1234567165374755859375
    當到了后面17位之后就開始不準確了
    因Double類型本身只能精確到17位
    但是如果這樣

    Java代碼

    1. BigDecimal n4  = new BigDecimal("1.23456789012345678901234567890E+9"); 
    BigDecimal n4  = new BigDecimal("1.23456789012345678901234567890E+9");
    

    結果是1234567891.12345678901234567890
    這樣就能絕對精確
    posted on 2010-01-27 12:06 liwei5891 閱讀(520) 評論(0)  編輯  收藏 所屬分類: Java

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲国产精品无码久久久蜜芽| 日本不卡高清中文字幕免费| A级毛片成人网站免费看| 一区二区三区在线免费| 成人A毛片免费观看网站| 久久99精品国产免费观看| 国产成人午夜精品免费视频| 国产成人免费片在线观看| 亚洲国产精品高清久久久| 成人电影在线免费观看| 日本免费v片一二三区| 亚洲欧美成aⅴ人在线观看| 暖暖免费中文在线日本| 免费观看久久精彩视频| 在线看片人成视频免费无遮挡| 亚洲人成网站在线观看青青| 亚洲福利在线观看| 亚洲av无码成人精品区一本二本 | 亚洲w码欧洲s码免费| 手机在线毛片免费播放| 亚洲熟妇无码另类久久久| 亚洲色偷偷色噜噜狠狠99| 一级特黄aa毛片免费观看| 亚洲最新中文字幕| 最近的2019免费中文字幕| 国产又黄又爽又猛的免费视频播放| 日韩亚洲综合精品国产| free哆啪啪免费永久| 亚洲av无一区二区三区| 国产成人亚洲精品91专区手机| 国产成人AV免费观看| 亚洲人妖女同在线播放| 日韩精品无码免费专区午夜不卡| 婷婷久久久亚洲欧洲日产国码AV | 四虎在线播放免费永久视频| 久久亚洲中文字幕精品有坂深雪 | 亚洲电影中文字幕| 嫩草影院免费观看| 中文字幕免费在线播放| 亚洲成a人片在线观看中文!!!| 免费观看国产精品|