Good, I have the following problem: I am making a query to a database, the query brings me a value type String with a format type ###. ###, ## the question I have are the following:
1.- How can I eliminate the commas and points to use that data in a mathematical operation within the app. (taking into account that the number in question may have decimals
2.- How can I get a new TextView after the operation to keep the format that I bring?
All this applied to a textWacther
saldito = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
saldo_despues.setText(addNumbers());
int ncom1, ncom2;
if(saldoUsrAct.getText().toString() != "" && saldoUsrAct.getText().length() > 0) {
ncom2 = Integer.parseInt(saldoUsrAct.getText().toString());
} else {
ncom2 = 0;
}
if(ttmonto.getText().toString() != "" && ttmonto.getText().length() > 0) {
ncom1 = Integer.parseInt(ttmonto.getText().toString());
} else {
ncom1 = 0;
}
if(ncom1 > ncom2){
saldo_despues.setTextColor(Color.parseColor("#FFE70822"));
} else {
saldo_despues.setTextColor(Color.parseColor("#000000"));
}
}
};
ttmonto.addTextChangedListener(saldito);
}
private String addNumbers() {
int number1;
int number2;
if(saldoUsrAct.getText().toString() != "" && saldoUsrAct.getText().length() > 0) {
number1 = Integer.parseInt(saldoUsrAct.getText().toString());
} else {
number1 = 0;
}
if(ttmonto.getText().toString() != "" && ttmonto.getText().length() > 0) {
number2 = Integer.parseInt(ttmonto.getText().toString());
} else {
number2 = 0;
}
return Integer.toString(number1 - number2);
}
}
This code works for me because I have not assigned the value to one of the textview, but at the moment it takes the variable that I bring from the database, it generates the error, as I can do to work with the chains that I bring.
Here I have another way that does not work either:
public void afterTextChanged(Editable s) {
String convertedString = new DecimalFormat("##.###.###,##").format(Double.parseDouble(addNumbers()));
saldo_despues.setText(convertedString);
double ncom1, ncom2;
if(saldoUsrAct.getText().toString() != "" && saldoUsrAct.getText().length() > 0) {
ncom2 = Double.parseDouble(saldoUsrAct.getText().toString());
} else {
ncom2 = 0;
}
if(ttmonto.getText().toString() != "" && ttmonto.getText().length() > 0) {
ncom1 = Double.parseDouble(ttmonto.getText().toString());
} else {
ncom1 = 0;
}
if(ncom1 > ncom2){
saldo_despues.setTextColor(Color.parseColor("#FFE70822"));
} else {
saldo_despues.setTextColor(Color.parseColor("#000000"));
}
}
};
ttmonto.addTextChangedListener(saldito);
}
private String addNumbers() {
double number1;
double number2;
if(saldoUsrAct.getText().toString() != "" && saldoUsrAct.getText().length() > 0) {
number1 = Double.parseDouble(saldoUsrAct.getText().toString());
} else {
number1 = 0;
}
if(ttmonto.getText().toString() != "" && ttmonto.getText().length() > 0) {
number2 = Double.parseDouble(ttmonto.getText().toString());
} else {
number2 = 0;
}
return Double.toString(number1 - number2);
}
Ok, I want to format a TextView, since I'm already figuring out how to bring from the server, a string that does not need to be modified in any way (a bit rudimentary but can work), what I need is to know how format the textview so that it shows me the formatted data the way I want.
Thank you in advance