【问题标题】:Bugs with Total Amount,Wrong Calculation & format总额错误、计算错误和格式错误
【发布时间】:2018-11-01 04:29:27
【问题描述】:

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

我的代码写错了吗? 这是我的代码

//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”,它不能显示十进制。它只会显示前面的数字。谁能帮帮我?

【问题讨论】:

    标签: android formatting calculation


    【解决方案1】:

    将 sum 变量的 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();
    }
    

    【讨论】:

    • 我更新了我的答案。在格式中输入#,###,###,###.00
    • 太棒了!编码愉快。
    【解决方案2】:

    解决办法是改变

    int
    

    double
    

    并将您的十进制格式更改为

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

    【讨论】:

    • 你也应该提到DecimalFormat
    猜你喜欢
    • 2019-03-22
    • 1970-01-01
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多