在Java中將double轉(zhuǎn)換為BigDecimal時需要注意哪些事項-創(chuàng)新互聯(lián)

今天就跟大家聊聊有關(guān)在Java中將double轉(zhuǎn)換為BigDecimal時需要注意哪些事項,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

創(chuàng)新互聯(lián)公司成立于2013年,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元中方做網(wǎng)站,已為上家服務(wù),為中方各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

先上結(jié)論:

不要直接用double變量作為構(gòu)造BigDecimal的參數(shù)。

線上有這么一段Java代碼邏輯:

1,接口傳來一個JSON串,里面有個數(shù)字:57.3。

2,解析JSON并把這個數(shù)字保存在一個float變量。

3,把這個float變量賦值給一個 BigDecimal對象,用的是BigDecimal的double參數(shù)的構(gòu)造:

new BigDecimal(double val)

4,把這個BigDecimal保存到MySQL數(shù)據(jù)庫,字段類型是decimal(15,2)。

這段代碼邏輯在線上跑了好久了,數(shù)據(jù)庫保存的值是57.3也沒什么問題,但是在今天debug的時候發(fā)現(xiàn),第三步的BigDecimal對象保存的值并不是57.3,而是57.299999237060546875,很明顯,出現(xiàn)了精度的問題。

至于數(shù)據(jù)庫最終保存了正確的57.3完全是因為字段類型設(shè)置為2位小數(shù),超過2位小數(shù)就四舍五入,所以才得到了正確的結(jié)果,相當(dāng)于MySQL給我們把這個精度問題掩蓋了。

總覺得這是個坑,所以研究了一下相關(guān)的知識。

首先是BigDecimal的double參數(shù)構(gòu)造,在官方JDK文檔中對這個構(gòu)造是這么描述的:

public BigDecimal(double val)

Translates a double into a BigDecimal which is the exact decimal representation of the double's binary floating-point value. The scale of the returned BigDecimal is the smallest value such that (10scale × val) is an integer.

Notes:

The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.

The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.

When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.

Parameters:

val - double value to be converted to BigDecimal.

Throws:

NumberFormatException - if val is infinite or NaN.

翻譯一下大概是這樣的:

1,BigDecimal(double val)構(gòu)造,用double當(dāng)參數(shù)來構(gòu)造一個BigDecimal對象。

2,但是這個構(gòu)造不太靠譜(unpredictable),你可能以為BigDecimal(0.1)就是妥妥的等于0.1,但是你以為你以為的就是你以為的?還真不是,BigDecimal(0.1)這貨實際上等于0.1000000000000000055511151231257827021181583404541015625,因為準(zhǔn)確的來說0.1本身不能算是一個double(其實0.1不能代表任何一個定長二進(jìn)制分?jǐn)?shù))。

3,BigDecimal(String val)構(gòu)造是靠譜的,BigDecimal(“0.1”)就是妥妥的等于0.1,推薦大家用這個構(gòu)造。

4,如果你非得用一個double變量來構(gòu)造一個BigDecimal,沒問題,我們貼心的提供了靜態(tài)方法valueOf(double),這個方法跟new Decimal(Double.toString(double))效果是一樣的。

說白了就是別直接拿double變量做參數(shù),好使用String類型做參數(shù)或者使用靜態(tài)方法valueOf(double),我寫了個例子試了一下:

public static void main(String[] args) { 
   float a=57.3f; 
   BigDecimal decimalA=new BigDecimal(a); 
   System.out.println(decimalA);   
 
   double b=57.3; 
   BigDecimal decimalB=new BigDecimal(b); 
   System.out.println(decimalB);   
 
   double c=57.3; 
   BigDecimal decimalC=new BigDecimal(Double.toString(c)); 
   System.out.println(decimalC);   
 
   double d=57.3; 
   BigDecimal decimalD=BigDecimal.valueOf(d); 
   System.out.println(decimalD); 
  }

輸出結(jié)果:

57.299999237060546875
57.2999999999999971578290569595992565155029296875
57.3
57.3

以后還是盡量按照官方推薦的套路來,否則不知道什么時候又給自己挖坑了。

補(bǔ)充:double轉(zhuǎn)bigDecimal精度問題

float的精度 : 2^23 7位

double的精度: 2^52 16位

十進(jìn)制 轉(zhuǎn) 二進(jìn)制 存在精度差

double g= 12.35;
BigDecimal bigG=new BigDecimal(g).setScale(1, BigDecimal.ROUND_HALF_UP); //期望得到12.4
System.out.println(“test G:”+bigG.doubleValue());
test G:12.3

原因:

定義double g= 12.35; 而在計算機(jī)中二進(jìn)制表示可能這是樣:定義了一個g=12.34444444444444449,

new BigDecimal(g) g還是12.34444444444444449
new BigDecimal(g).setScale(1, BigDecimal.ROUND_HALF_UP); 得到12.3

正確的定義方式是使用字符串構(gòu)造函數(shù):

new BigDecimal(“12.35”).setScale(1, BigDecimal.ROUND_HALF_UP)

首先得從計算機(jī)本身去討論這個問題。我們知道,計算機(jī)并不能識別除了二進(jìn)制數(shù)據(jù)以外的任何數(shù)據(jù)。無論我們使用何種編程語言,在何種編譯環(huán)境下工作,都要先 把源程序翻譯成二進(jìn)制的機(jī)器碼后才能被計算機(jī)識別。以上面提到的情況為例,我們源程序里的2.4是十進(jìn)制的,計算機(jī)不能直接識別,要先編譯成二進(jìn)制。但問 題來了,2.4的二進(jìn)制表示并非是精確的2.4,反而最為接近的二進(jìn)制表示是2.3999999999999999。原因在于浮點數(shù)由兩部分組成:指數(shù)和尾數(shù),這點如果知道怎樣進(jìn)行浮點數(shù)的二進(jìn)制與十進(jìn)制轉(zhuǎn)換,應(yīng)該是不難理解的。如果在這個轉(zhuǎn)換的過程中,浮點數(shù)參與了計算,那么轉(zhuǎn)換的過程就會變得不可預(yù) 知,并且變得不可逆。我們有理由相信,就是在這個過程中,發(fā)生了精度的丟失。而至于為什么有些浮點計算會得到準(zhǔn)確的結(jié)果,應(yīng)該也是碰巧那個計算的二進(jìn)制與 十進(jìn)制之間能夠準(zhǔn)確轉(zhuǎn)換。而當(dāng)輸出單個浮點型數(shù)據(jù)的時候,可以正確輸出,如

double d = 2.4;
System.out.println(d);

輸出的是2.4,而不是2.3999999999999999。也就是說,不進(jìn)行浮點計算的時候,在十進(jìn)制里浮點數(shù)能正確顯示。這更印證了我以上的想法,即如果浮點數(shù)參與了計算,那么浮點數(shù)二進(jìn)制與十進(jìn)制間的轉(zhuǎn)換過程就會變得不可預(yù)知,并且變得不可逆。

事實上,浮點數(shù)并不適合用于精確計算,而適合進(jìn)行科學(xué)計算。這里有一個小知識:既然float和double型用來表示帶有小數(shù)點的數(shù),那為什么我們不稱 它們?yōu)椤靶?shù)”或者“實數(shù)”,要叫浮點數(shù)呢?因為這些數(shù)都以科學(xué)計數(shù)法的形式存儲。當(dāng)一個數(shù)如50.534,轉(zhuǎn)換成科學(xué)計數(shù)法的形式為5.053e1,它 的小數(shù)點移動到了一個新的位置(即浮動了)。可見,浮點數(shù)本來就是用于科學(xué)計算的,用來進(jìn)行精確計算實在太不合適了。

在《Effective Java》這本書中也提到這個原則,float和double只能用來做科學(xué)計算或者是工程計算,在商業(yè)計算中我們要用java.math.BigDecimal。使用BigDecimal并且一定要用String來夠造。

BigDecimal用哪個構(gòu)造函數(shù)?

BigDecimal(double val)
BigDecimal(String val)

看完上述內(nèi)容,你們對在Java中將double轉(zhuǎn)換為BigDecimal時需要注意哪些事項有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

分享文章:在Java中將double轉(zhuǎn)換為BigDecimal時需要注意哪些事項-創(chuàng)新互聯(lián)
分享地址:http://muchs.cn/article40/cdcgho.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、企業(yè)建站、服務(wù)器托管搜索引擎優(yōu)化、App設(shè)計商城網(wǎng)站

廣告

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

外貿(mào)網(wǎng)站制作