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

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

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

    about BigDecimal

    在java中做大數(shù)運(yùn)算時(shí)要用到BigDecimal類。
    何謂大數(shù)?
        單精度浮點(diǎn)數(shù):float--32位(4字節(jié))--有效數(shù)字7位;
        雙精度浮點(diǎn)數(shù):double--64位(8字節(jié))--有效數(shù)字16位;
        超過(guò)double表示范圍的,一律用BigDecimal。
    關(guān)于BigDecimal的構(gòu)造,需要從String構(gòu)造,切記不可由double構(gòu)造
        即不可用 new BigDecimal(double var) [X]
        而是通過(guò) new BigDecimal(String var)
        原因參考http://hi.baidu.com/waiting__for__you/blog/item/967206ec863751d3b21cb170.html

        BigDecimal(double)是把一個(gè)double類型十進(jìn)制數(shù)構(gòu)造為一個(gè)BigDecimal對(duì)象實(shí)例。

    BigDecimal(String)是把一個(gè)以String表示的BigDecimal對(duì)象構(gòu)造為BigDecimal對(duì)象實(shí)例。

    習(xí)慣上,對(duì)于浮點(diǎn)數(shù)我們都會(huì)定義為double或float,但BigDecimal API文檔中對(duì)于BigDecimal(double)有這么一段話:

    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 .10000000000000000555111512312578 27021181583404541015625. 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 notwithstanding.

    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

    下面對(duì)這段話做簡(jiǎn)單解釋:

    注意:這個(gè)構(gòu)造器的結(jié)果可能會(huì)有不可預(yù)知的結(jié)果。有人可能設(shè)想new BigDecimal(.1)等于.1是正確的,但它實(shí)際上是等于.1000000000000000055511151231257827021181583404541015625,這就是為什么.1不能用一個(gè)double精確表示的原因,因此,這個(gè)被放進(jìn)構(gòu)造器中的長(zhǎng)值并不精確的等于.1,盡管外觀看起來(lái)是相等的。

    然而(String)構(gòu)造器,則完全可預(yù)知的,new BigDecimal(“.1”)如同期望的那樣精確的等于.1,因此,(String)構(gòu)造器是被優(yōu)先推薦使用的。

    看下面的結(jié)果:

         System.out.println(new BigDecimal(123456789.02).toString());

          System.out.println(new BigDecimal("123456789.02").toString());

    輸出為:

    123456789.01999999582767486572265625

    123456789.02

    現(xiàn)在我們知道,如果需要精確計(jì)算,非要用String來(lái)夠造BigDecimal不可!





     

    posted on 2012-02-15 16:50 輕帆向南 閱讀(303) 評(píng)論(0)  編輯  收藏 所屬分類: oj


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

    留言簿

    隨筆分類(13)

    隨筆檔案(13)

    文章分類

    最新評(píng)論

    主站蜘蛛池模板: 亚洲国产精品特色大片观看完整版| 亚洲欧洲日产国产综合网| 免费又黄又硬又爽大片| 亚洲熟妇av一区二区三区漫画| 亚洲春色在线视频| 国产久爱免费精品视频| 国产精彩免费视频| 91午夜精品亚洲一区二区三区| 最刺激黄a大片免费网站| 亚洲精品午夜在线观看| xxxx日本免费| 亚洲人xxx日本人18| 精品久久久久久国产免费了 | 午夜在线免费视频| 99re6热视频精品免费观看| 亚洲avav天堂av在线不卡| 一级毛片免费观看不卡视频| 国产乱子影视频上线免费观看| 亚洲精品永久在线观看| 无码精品国产一区二区三区免费 | 久久精品国产亚洲一区二区| 国产一区二区三区免费| 亚洲午夜未满十八勿入| 99久久久精品免费观看国产| 亚洲欧美日韩中文字幕在线一区| 日韩人妻无码免费视频一区二区三区| 美女羞羞喷液视频免费| 国产成人无码综合亚洲日韩| 51在线视频免费观看视频| 亚洲精品国偷自产在线| 91青青青国产在观免费影视| 亚洲av永久无码嘿嘿嘿| 91成人在线免费观看| 亚洲一卡2卡3卡4卡5卡6卡| 亚洲高清国产拍精品青青草原| 野花香在线视频免费观看大全| 亚洲天堂2017无码中文| 大地影院MV在线观看视频免费 | 亚洲va成无码人在线观看| www.91亚洲| 亚洲色少妇熟女11p|