【发布时间】:2011-03-07 14:02:06
【问题描述】:
我目前正在开发一款适用于 Android 的应用。在这个应用程序中,我需要将计算出的数字转换为百分比。原始数字 (tipTotal) 计算正确,但是当我尝试通过除以 100 (tipPercentage) 转换为十进制时,tipPercentage 始终为 0.0。
我已确认tipTotal 正确且符合我的预期。
public void onClick(View view) {
int tipTotal = 0;
float totalBill = 0;
float tipPercentage = 0;
String time, attitude, refills, clearing, accuracy, bill;
time = timeSpinner.getSelectedItem().toString();
attitude = attitudeSpinner.getSelectedItem().toString();
refills = refillsSpinner.getSelectedItem().toString();
clearing = clearingSpinner.getSelectedItem().toString();
accuracy = accuracySpinner.getSelectedItem().toString();
bill = txtBill.getText().toString();
tipTotal = Integer.parseInt(time)+Integer.parseInt(attitude)+Integer.parseInt(refills)+Integer.parseInt(clearing)+Integer.parseInt(accuracy);
tipPercentage = tipTotal / 100;
Context context = getApplicationContext();
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, String.valueOf(tipPercentage), duration);
toast.show();
}
感谢您的帮助!
【问题讨论】: