Reputation: 21
I am working on a proration calculator app for Android. When I run the code below, I get an incorrect result.
public void calc(View v)
{
double myAmount = Double.parseDouble(amount.getText().toString());
double myDaysLeft = Double.parseDouble(daysLeft.getText().toString());
myAmount = Double.parseDouble(amount.getText().toString());
myDaysLeft = Double.parseDouble(amount.getText().toString());
double calcResult = (myAmount /30) * myDaysLeft;
String tot = new Double(calcResult).toString();
result.setText(tot);
The values I am entering are myAmount = 25.99 myDaysLeft = 10. When I run it in my app I get 22.51. The correct result should be 8.66. Can anyone help me determine where the error lies? Thanks in advance!
Upvotes: 0
Views: 42
Reputation: 57
myDaysLeft = Double.parseDouble(amount.getText().toString());
You are using ammount
here when it should me myDaysLeft
Upvotes: 1
Reputation: 24417
myDaysLeft = Double.parseDouble(amount.getText().toString());
should be
myDaysLeft = Double.parseDouble(daysLeft.getText().toString());
However, you can just delete the 3rd and 4th lines as you are already setting the variables in the 1st and 2nd lines when you declare them. Why assign them twice?
Upvotes: 0