我希望对检索到的数字进行计数,并找到其相对于fullAttendance int的百分比。这是我的代码:
attendanceButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attendanceRef.whereEqualTo("sessionID", "5I0PLcpgmwdcjKLYLgkU").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
int fullAttendance = 12;
int count = 0;
for (DocumentSnapshot document : task.getResult()) {
count++;
attendanceNumbs = findViewById(R.id.attendanceNums);
attendanceNumbs.setText((count/fullAttendance)*100 + " %" + "\n Attendance for this session");
}
Log.d("TAG", count + "");
} else {
}
}
});
答案 0 :(得分:0)
fullAttendance
和count
变量都是整数,因此它们的除法结果也将是整数。您应将其中一个操作数声明为float
(双精度),或将其中一个明确地强制除法,如下所示:
count/(float)fullAttendance