問題描述
我將一些數據存儲在數據庫中,并使用游標讀取這些數據.所有數據均為 56.45 , 3.04, 0.03 類型,所以小數點后有兩位數.現在我想對它們求和,但這似乎并不容易......我用 c.getDouble(3)
得到這些數字,然后我將它添加到 sum 變量中,例如:
I store some data in a database and I read those data with a cursor. All the data are 56.45 , 3.04, 0.03 type, so two numbers after the decimal point. Now I would like to sum them but it doesnt seem to be easy... I get those number with c.getDouble(3)
then I add it to the sum variable like:
sum+= c.getDouble(4) * multi
multi 是 1 到 100 之間的整數.現在我的問題是,在得到 sum 變量后,我得到一個數字,例如:59.51999999999999999.我沒有發布代碼,但除了 string.valueOf
用于使數字出現在我的小部件上之外,沒有任何轉換.所以有人經歷過嗎?據我計算,它應該是 59.52,但我得到的數字是 99999……為什么會這樣?(有趣的是,使用 Math.round()
不起作用,即使我設置 (sum)*100/100 ,我也會得到 59.0 ..提前致謝!
multi is an integer between 1 and 100. Now my problem is that, after getting the sum variable I get a number like: 59.51999999999999999. I do not post a code, but there is no conversion except a string.valueOf
for making the number appear on my widget. So anybody experienced this? As I count it should be 59.52, but instead I get that number with 99999... Why is that happening? (funny thing that with a Math.round()
does not work, I get 59.0 even if I set (sum)*100 / 100..
Thanks in advance!
推薦答案
其他方法,你可以定義你的 sum 變量為 double
然后你可以為你的設置 NumberFormat
總和變量.
Other approach, you can define your sum variable as double
and then you can set NumberFormat
for your sum variable.
示例:
double sum = 21.2015;
NumberFormat formatter = new DecimalFormat("#.##");
formatter.format(sum);
這篇關于Android 數字格式在某種程度上是錯誤的,而不是 3.5 我得到 3.499999999,為什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!