国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Java double轉(zhuǎn)BigDecimal的注意事項說明

瀏覽:59日期:2022-08-18 09:08:39
先上結(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é)果,相當于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當參數(shù)來構(gòu)造一個BigDecimal對象。

2,但是這個構(gòu)造不太靠譜(unpredictable),你可能以為BigDecimal(0.1)就是妥妥的等于0.1,但是你以為你以為的就是你以為的?還真不是,BigDecimal(0.1)這貨實際上等于0.1000000000000000055511151231257827021181583404541015625,因為準確的來說0.1本身不能算是一個double(其實0.1不能代表任何一個定長二進制分數(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.29999923706054687557.299999999999997157829056959599256515502929687557.357.3

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

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

float的精度 : 2^23 7位

double的精度: 2^52 16位

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

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

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

new BigDecimal(g) g還是12.34444444444444449new BigDecimal(g).setScale(1, BigDecimal.ROUND_HALF_UP); 得到12.3正確的定義方式是使用字符串構(gòu)造函數(shù):

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

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

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

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

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

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

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

BigDecimal(double val)BigDecimal(String val)

上面的API簡要描述相當?shù)拿鞔_,而且通常情況下,上面的那一個使用起來要方便一些。我們可能想都不想就用上了,會有什么問題呢?等到出了問題的時候,才發(fā)現(xiàn)參數(shù)是double的構(gòu)造方法的詳細說明中有這么一段:

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.

原來我們?nèi)绻枰_計算,非要用String來夠造BigDecimal不可!

簡單來說 精確計算 ,需要用到bigDeicmal的String 構(gòu)造

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標簽: Java
相關(guān)文章:
主站蜘蛛池模板: 欧美α一级毛片 | 性生活免费视频网站 | a一级免费 | 大片在线播放日本一级毛片 | 国产三级日本三级美三级 | 成人毛片视频免费网站观看 | 亚洲免费观看在线视频 | 亚洲视频手机在线 | 国产精品亚洲精品日韩已满 | 日韩一区二区三区精品 | 欧美最爽乱淫视频播放黑人 | 大陆老太xxxxxxxxhd | 特级aa一级欧美毛片 | 亚洲欧美日韩国产专区一区 | 草草影院www色极品欧美 | 亚洲成人在线播放 | 欧洲一级毛片 | 亚洲成人影院在线 | 欧美一级在线毛片免费观看 | 91精品福利手机国产在线 | 欧美大狠狠大臿蕉香蕉大视频 | 色偷偷88欧美精品久久久 | 久久综久久美利坚合众国 | 久久精品国产免费看久久精品 | 99久久国产综合精品成人影院 | 草草视频在线观看 | 久久久亚洲欧洲日产国码二区 | 一级黄色片aaa | 欧美一区二区在线免费观看 | 综合久| 日本高清va不卡视频在线观看 | 日韩三级在线观看 | 欧美三级美国一级 | 精品国产一区二区三区四区vr | 欧美第一精品 | 国产精品大片天天看片 | 国产亚洲欧美另类久久久 | 中文字幕在线免费观看视频 | 久久久香蕉视频 | 亚洲三级成人 | 国产亚洲一区二区在线观看 |