Java高精度計(jì)算BigDecimal類實(shí)例分析

這篇文章主要介紹了Java高精度計(jì)算BigDecimal類實(shí)例分析的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Java高精度計(jì)算BigDecimal類實(shí)例分析文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

專注于為中小企業(yè)提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)集美免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

如果我們編譯運(yùn)行下面這個(gè)程序會(huì)看到什么?

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);

   }

};你沒有看錯(cuò)!結(jié)果確實(shí)是 0.060000000000000005 0.5800000000000001 401.49999999999994 1.2329999999999999 Java中的簡(jiǎn)單浮點(diǎn)數(shù)類型float和double不能夠進(jìn)行運(yùn)算。

不光是Java,在其它很多編程語(yǔ)言中也有這樣的問(wèn)題。在大多數(shù)情況下,計(jì)算的結(jié)果是準(zhǔn)確的,但是多試幾次(可以做一個(gè)循環(huán))就可以試出類似上面的錯(cuò)誤。

現(xiàn)在終于理解為什么要有BCD碼了。這個(gè)問(wèn)題相當(dāng)嚴(yán)重,如果你有9.999999999999元,你的計(jì)算機(jī)是不會(huì)認(rèn)為你可以購(gòu)買10元的商品的。

在有的編程語(yǔ)言中提供了專門的貨幣類型來(lái)處理這種情況,但是Java沒有?,F(xiàn)在讓我們看看如何解決這個(gè)問(wèn)題。

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

public double round(double value){

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

}非常不幸,上面的代碼并不能正常工作,給這個(gè)方法傳入4.015它將返回4.01而不是4.02,如我們?cè)谏厦婵吹降?4.015*100=401.49999999999994

因此如果我們要做到精確的四舍五入,不能利用簡(jiǎn)單類型做任何運(yùn)算也不能解決這個(gè)問(wèn)題:

System.out.println(new java.text.Decimal Format("0.00").format(4.025));輸出是4.02

[@more@]

Big Decimal在《Effective Java》這本書中也提到這個(gè)原則,float和double只能用來(lái)做科學(xué)計(jì)算或者是工程計(jì)算,

在商業(yè)計(jì)算中我們要用java.math.Big Decimal?。Big Decimal一共有4個(gè)夠造方法,我們不關(guān)心用Big Integer?來(lái)夠造的那兩個(gè),那么還有兩個(gè),

它們是: Big Decimal(double val) Translates a double into a Big Decimal.

Big Decimal(String val) Translates the String repre sentation of a Big Decimal into a Big Decimal.

上面的API簡(jiǎn)要描述相當(dāng)?shù)拿鞔_,而且通常情況下,上面的那一個(gè)使用起來(lái)要方便一些。我們可能想都不想就用上了,會(huì)有什么問(wèn)題呢?

等到出了問(wèn)題的時(shí)候,才發(fā)現(xiàn)上面哪個(gè)夠造方法的詳細(xì)說(shuō)明中有這么一段: Note: the results of this constructor can be somewhat unpredictable.

One might assume that new Big Decimal(.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 Big Decimal(".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.

原來(lái)我們?nèi)绻枰_計(jì)算,非要用String來(lái)夠造Big Decimal不可!在《Effective Java》一書中的例子是用String來(lái)夠造Big Decimal的,

但是書上卻沒有強(qiáng)調(diào)這一點(diǎn),這也許是一個(gè)小小的失誤吧。

解決方案現(xiàn)在我們已經(jīng)可以解決這個(gè)問(wèn)題了,原則是使用Big Decimal并且一定要用String來(lái)夠造。但是想像一下吧,如果我們要做一個(gè)加法運(yùn)算,

需要先將兩個(gè)浮點(diǎn)數(shù)轉(zhuǎn)為String,然后夠造成Big Decimal,在其中一個(gè)上調(diào)用add方法,傳入另一個(gè)作為參數(shù),然后把運(yùn)算的結(jié)果(Big Decimal)再轉(zhuǎn)換為浮點(diǎn)數(shù)。

你能夠忍受這么煩瑣的過(guò)程嗎?下面我們提供一個(gè)工具類Arith來(lái)簡(jiǎn)化操作。它提供以下靜態(tài)方法,包括加減乘除和四舍五入:

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)

關(guān)于“Java高精度計(jì)算BigDecimal類實(shí)例分析”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Java高精度計(jì)算BigDecimal類實(shí)例分析”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站標(biāo)題:Java高精度計(jì)算BigDecimal類實(shí)例分析
文章網(wǎng)址:http://muchs.cn/article44/iiogee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航網(wǎng)站設(shè)計(jì)、虛擬主機(jī)、網(wǎng)站內(nèi)鏈、網(wǎng)頁(yè)設(shè)計(jì)公司、手機(jī)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)