i have the following code. Whenever try to use numbers with decimals on my app crashes. I know is something wrong with my final result but whats wrong???
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String f = et1.getText().toString();
int i = Integer.parseInt(f);
String s = et2.getText().toString();
int j = Integer.parseInt(s);
String w = et4.getText().toString();
int q = Integer.parseInt(s);
int price_gold = 10;
int fpa = 10;
int fpol = 10;
int isot = 10;
int sint_ker = 10;
Integer result1 = (i * price_gold) + (j * 1000) + (q * isot);
result = result1 / 340;
String res = result.toString();
et3.setText(res);
}
}
You should use Float.parseFloat but be careful of empty text, something like the following
String f = et1.getText().toString();
if (!TextUtils.isEmpty(f)) {
Float i = Float.parseFloat(f);
}
if you want to have integer output as your answer, have all your values except result1 as doubles, much easier check below:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String f = et1.getText().toString();
double i = Double.parseDouble(f);
String s = et2.getText().toString();
double j = Double.parseDouble(s);
String w = et4.getText().toString();
double q = Double.parseDouble(s);
double price_gold = 10;
double fpa = 10;
double fpol = 10;
double isot = 10;
double sint_ker = 10;
Integer result1 = ((int)i * price_gold) + ((int)j * 1000) + ((int)q * isot);
result = result1 / 340;
String res = String.valueOf(result);
et3.setText(res);
}
This code should then give you your integer values you are looking for, Easy :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.