总金额,计算和格式错误的错误

时间:2018-11-01 04:29:28

标签: android formatting calculation

我可以知道这是什么问题吗?它应该是“ 5555555556”。看到总金额不正确。

enter image description here

我的代码是否写错了? 这是我的验证码

//show total amount
        try {
            SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
            Cursor cursor = db.rawQuery("SELECT SUM (Amount) from Donation_Details WHERE strftime('%d %m %Y',CreatedDate) = strftime('%d %m %Y',date('now'))", null);
            if (cursor.moveToFirst()) {

                int sum = cursor.getInt(0);
                DecimalFormat formatter = new DecimalFormat("#,###,###");
                String yourFormattedString = formatter.format(sum);

                total_Amount.setText("RM " + yourFormattedString);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

如果我写了“ 555555.55”,它就不能显示十进制。它只会显示前线号码。谁能帮我吗?

2 个答案:

答案 0 :(得分:1)

解决方案是改变

int

double

并将十进制格式更改为

DecimalFormat formatter = new DecimalFormat("#,###,###,###");

答案 1 :(得分:1)

将总和变量的int更改为double,还像下面的代码一样更改DecimalFormat

try {
    SQLiteDatabase db = mSQLiteHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT SUM (Amount) from Donation_Details WHERE strftime('%d %m %Y',CreatedDate) = strftime('%d %m %Y',date('now'))", null);
    if (cursor.moveToFirst()) {
        double sum = cursor.getDouble(0);
        DecimalFormat formatter = new DecimalFormat("#,###,###,###.00");
        String yourFormattedString = formatter.format(sum);
        total_Amount.setText("RM " + yourFormattedString);
    }
} catch (Exception e) {
    e.printStackTrace();
}