The shared preference putFlot does not allow decimal point calculation by division of integers during the operation.

Asked 2 years ago, Updated 2 years ago, 72 views

Both activities share computational results as shared preferences.

If you press the correct view in the activity where the four-point multiple questions are printed,

The values of the keys totaltry and successstry in the shared preference increase by 1.

When I printed out the toast message, the values of totaltry and successtry are increasing well.

editor.putFloat("successrate", (setting.getInt("successtry", 0) / setting.getInt("totaltry", 0)));

In this calculation, it continues to be 0ㅠ<

Why is there no putDouble in the shared preference?

How can I do a division operation?

'''

        if     (v.getTag().equals(tagAnswer)) {

        editor.putInt("totaltry", setting.getInt("totaltry", 0) + 1);
        editor.putInt("successtry", setting.getInt("successtry", 0) + 1);
        editor.commit();
        editor.putFloat("successrate", (setting.getInt("successtry", 0) / setting.getInt("totaltry", 0))                                                                                             );
        editor.commit();

        Toast.makeText(getApplicationContext(), "totaltry = "+ setting.getInt("totaltry",0)+
                " " successtry = "+ setting.getInt("successtry",0) +
                " " successrate = "+ setting.getFloat("successrate",0) , Toast.LENGTH_LONG).show();

        switch (setting.getInt("level", 1)) {
            case 1:
                if (setting.getFloat("exp_a_rate", 0) >= 1) {
                    editor.putInt("level", setting.getInt("level", 1) + 1);
                    editor.putInt("totaltry", 0);
                    editor.putInt("successtry", 0);
                    editor.putFloat("successrate", 0);
                    editor.commit();
                } } else {
                    editor.putInt("exp_a", setting.getInt("exp_a", 0) + 1);
                    editor.commit();
                    editor.putFloat("exp_a_rate", setting.getInt("exp_a", 0) / 5);
                    editor.commit();
                }
                break;
   .....

'''

android calculate decimal-point

2022-09-22 14:42

1 Answers

Cast the getInt value as float and calculate it.

editor.putFloat("successrate", ( (float)setting.getInt("successtry", 0) / (float) setting.getInt("totaltry", 0)));


2022-09-22 14:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.