All-
I am having a hard time taking the value of an EditText and messing with it to preform equations and tasks like rounding and multiplying. I have heard that BigDecimal is a good way to do this but it was very difficult to get the variables to a type where they could become BigDecimal variables (View->String->Float->BigDecimal) and then to get them to a type where they could be outputted (BigDecimal->?).
Here is my code:
public class TippingActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void calculateNumbers(View view) throws ParseException {
// Bill
EditText text = (EditText)findViewById(R.id.edit_bill);
String value;
value = text.getText().toString();
float bill = Float.parseFloat(value);
BigDecimal billbd;
billbd = new BigDecimal(bill);
//Tip
EditText text2 = (EditText)findViewById(R.id.edit_tip);
String value2;
value2 = text2.getText().toString();
if(!value2.contains("."))
value2 = "."+value2;
float tip = Float.parseFloat(value2);
BigDecimal tipbd;
tipbd = new BigDecimal(tip);
//People
EditText text3 = (EditText)findViewById(R.id.edit_people);
String value3;
value3 = text3.getText().toString();
float people = Float.parseFloat(value3);
BigDecimal peoplebd;
peoplebd = new BigDecimal(people);
//Answer
TextView answer = (TextView) findViewById(R.id.answer);
//Answer to string
String answerstring;
answerstring = answer.getText().toString();
//Answer to Float
float answerfloat = Float.parseFloat(answerstring);
//Answer to BigDecimal
BigDecimal answerbig;
answerbig = new BigDecimal(answerfloat);
answerbig.setScale(2, RoundingMode.HALF_UP);
answerbig = billbd.multiply(tipbd).add(billbd).divide(peoplebd);
//Answer to String
String answerfrombd = answerbig.getText().toString();
answer.setText(answerfrombd);
}
}
In the second line of code from the bottom I get an error on
`String answerfrombd = answerbig.getText().toString();`
because I can’t go from BigDecimal to String. Any ideas?
Thanks in advance!
You can do, depending how you want it to display.
or: