쉐어드프리퍼런스 putFloat으로 연산 중 정수끼리 나눗셈에 의한 소수점 계산이 안됩니다.
조회수 1086회
두 액티비티에서 연산 결과를 쉐어드프리퍼런스로 공유하고 있습니다.
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 답변
-
getInt 한 값을 float 으로 캐스팅해서 연산해보세요.
editor.putFloat("successrate", ( (float)setting.getInt("successtry", 0) / (float) setting.getInt("totaltry", 0)));
댓글 입력