嗨,我想這樣的TextView級別可以把小數點,但我不知道該怎麼做,任何人有一個想法?如果它只輸出1,但我希望輸出1.80。 :)用文本顯示小數點
public class Main extends Activity {
int counter;
EditText weight, hours;
TextView amount, level;
Button calcuate;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = 0;
weight = (EditText) findViewById(R.id.weight);
hours = (EditText) findViewById(R.id.hours);
amount = (TextView) findViewById(R.id.amount);
level = (TextView) findViewById(R.id.alcohol_level);
calcuate = (Button) findViewById(R.id.calcuate);
final String widmark = getResources().getString(
R.string.widmark);
final String hundra = getResources().getString(
R.string.hundra);
final String cl = getResources().getString(
R.string.cl);
calcuate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Integer wid, mgs;
String w = weight.getText().toString();
String h = hours.getText().toString();
wid = Integer.parseInt(w) * Integer.parseInt(widmark)/Integer.parseInt(hundra);
mgs = Integer.parseInt(cl)/Integer.parseInt(wid.toString())/Integer.parseInt(hundra);
level.setText(mgs.toString());
}
});
}
}
您必須使用浮點數進行算術運算,然後使用'DecimalFormat'格式化結果,以保持所需的精度。 – neevek 2012-08-12 16:14:18