쉐어드프리퍼런스 putFloat으로 연산 중 정수끼리 나눗셈에 의한 소수점 계산이 안됩니다.

조회수 1064회

두 액티비티에서 연산 결과를 쉐어드프리퍼런스로 공유하고 있습니다.

4지선다 문제가 출력되는 액티비티에서 정답인 뷰를 누르면

쉐어드프리퍼런스의 key인 totaltry와 successtry의 값이 1씩 증가합니다.

토스트메시지로 출력해보니 totaltry와 successtry의 값은 잘 증가하고있는데

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

이 연산에서 계속 0.0이 됩니다ㅠㅠ

쉐어드프리퍼런스에는 왜 putDouble이 없나요ㅠㅠ

어떻게해야 나눗셈 연산을 할 수 있을까요??

'''

        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;
   .....

'''

1 답변

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)