Hi i want make so the TextView level can put out decimal but i don’ty know how to do that any one got an idea? NOw it only puts out 1 but i want it to put out 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());
}
});
}
}
intdivision will only produceint‘s. If you want your output to be offloatordoubletype, you must usedoubleorfloatdivision.Note that not all variables considered in the expression need to be
double‘s, only one of them does.Another thing that is noteworthy here is whether or not you want two decimal places (assuming a currency here). By default, Java/Android will only spit out as many decimal places as necessary.
1.80will display as1.8. To alleviate this, you should use aNumberFormat(specifically, useNumberFormat.getCurrencyInstance()) so that you can specify that you want the number of decimals for your defaultLocale‘s currency.