I want to get 5% of any number and it has occurred to me in the following way:
public class Main4Activity extends AppCompatActivity {
EditText uno;
TextView tres, tresinvisible, multiplica, divide, haciendocuenta, haciendocuenta2;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
uno = (EditText) findViewById(R.id.uno);
tres = (TextView) findViewById(R.id.tres);
tresinvisible = (TextView) findViewById(R.id.tresinvisible);
multiplica = (TextView) findViewById(R.id.multiplica);
divide = (TextView) findViewById(R.id.divide);
haciendocuenta = (TextView) findViewById(R.id.haciendocuenta);
haciendocuenta2 = (TextView) findViewById(R.id.haciendocuenta2);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int aux0 = Integer.valueOf(uno.getText().toString());
int resultado = aux0;
// envio el número a:
tres.setText("" + resultado + " " + "es el resultado final");
tresinvisible.setText("" + resultado);
// aux2 es el número que escribo en mi editText para sacarle el 5%
int aux2 = Integer.valueOf(tresinvisible.getText().toString());
// aux3 es igual a 5
int aux3 = Integer.valueOf(multiplica.getText().toString());
// multiplico aux2 * aux3
int casiresultado1 = aux2 * aux3;
// envio el resultado a:
haciendocuenta.setText("" + casiresultado1);
//
int aux4 = Integer.valueOf(haciendocuenta.getText().toString());
// divide es igual a 100
int aux5 = Integer.valueOf(divide.getText().toString());
// divido aux4 * aux5
int resultadofinal11 = aux4 / aux5;
// envio el resultado a:
haciendocuenta2.setText("" + resultadofinal11);
//
int aux6 = Integer.valueOf(tresinvisible.getText().toString());
int aux7 = Integer.valueOf(haciendocuenta2.getText().toString());
// resto aux6 - aux7
int resultadofinal = aux6 - aux7;
// envio el resultado final a:
tres.setText("" + resultadofinal + " " + "es el resultado final");
}
});
}
}
I imagine that it is not the best way, but it has worked well for me, I have left notes of how I have done each action so that the code is clearer, but of course my problem comes now. As you can see in my code the number from which I get 5% I write it in my EditText
but if it is a number with decimals the application closes. How can I solve it and be able to get 5% off numbers with decimals too?
Thanks! And if you know how I could make a cleaner / shorter code I would also thank you, it's like I thought I could do it but I do not know if it's the best way ...
The logcat
marks this when it stops:
java.lang.NumberFormatException: Invalid int: "570.5"
EDITO1:
public class Main4Activity extends AppCompatActivity {
EditText uno;
TextView tres;
Button btn1;
static final BigDecimal PORCENTAJE_CINCO = new BigDecimal("0.05");
public BigDecimal calculaCincoPorCiento(BigDecimal numero) {
return numero.multiply(PORCENTAJE_CINCO);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
uno = (EditText) findViewById(R.id.uno);
tres = (TextView) findViewById(R.id.tres);
btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BigDecimal aux0 = new BigDecimal(uno.getText().toString());
BigDecimal aux1 = new BigDecimal(String.valueOf(calculaCincoPorCiento(PORCENTAJE_CINCO)));
aux1 = aux1.divide(aux0);
tres.setText("" + aux1 + " " + "es el 5%");
}
});
}
}
I have tried to get 5% with decimals, seeing the response of @LuiggiMendoza, but I do not get 5%, I guess I do it wrong. Can you help me? Thanks!