本文介紹了Java雙精度求和麻煩的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想知道為什么會出現此錯誤.(這是Eclipse調試的顯示日志)
I would like to know why I get this error. (this is Display log of Eclipse debug)
var
(double) 2.8
tot.getIva()
(java.lang.Double) 0.17
var+tot.get()
(double) 2.9699999999999998
我不明白為什么我沒有得到簡單的 2.97!
I can not understand why I did not get simply 2.97!
推薦答案
如果你想要 2.97
,你應該使用 BigDecimal
.
If you wanted 2.97
, you should have used BigDecimal
.
double
s 在 binary 中存儲為分數,而不是十進制.例如,3.75
只是存儲為 2^1 + 2^0 + 2^(-1) + 2^(-2)
.
double
s are stored as fractions in binary, not decimal. So 3.75
, for example, is just stored as 2^1 + 2^0 + 2^(-1) + 2^(-2)
.
2.8
和 0.17
不能完全表示為二進制分數,所以會有一些舍入誤差.
2.8
and 0.17
cannot be represented exactly as binary fractions, so there's going to be some rounding error.
這篇文章可能對您也有幫助.
這篇關于Java雙精度求和麻煩的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!