gpt4 book ai didi

java - 如何在 Java 中对 double 进行舍入? (安卓开发)

转载 作者:行者123 更新时间:2023-12-02 08:05:54 25 4
gpt4 key购买 nike

我正在使用以下方法从 SQLite 数据库返回值。

public String Totalsponsor() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_SPONSOR, KEY_SWIMMERLAPS };
Cursor c = ourDatabase.rawQuery("SELECT " + KEY_SWIMMERLAPS + " * "
+ KEY_SPONSOR + " AS result FROM " + DATABASE_TABLE + " ORDER BY "+ KEY_SWIMMERLAPS+" DESC", null);

String result = "";

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + c.getDouble(0) + "\n";
}

return result;

}

在返回结果之前如何舍入结果?

我尝试过:

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
结果 = 结果 + Math.round(c.getDouble(0)) + "\n";
}

但它似乎不起作用(也许我还没有理解舍入方法?)。

编辑:我似乎只得到了第一个结果。

最佳答案

DecimalFormat df = new DecimalFormat("####.##"); // here define you can define how much precision you want.

for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result = result + df.format(c.getDouble(0)) + "\n";
}

关于java - 如何在 Java 中对 double 进行舍入? (安卓开发),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8180961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com