Logical operators with different value

Asked

Viewed 46 times

-1

nome_adc_1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       double percent =  (estimatedFare*0.20);
       double soma = estimatedFare + percent;
       double subtrai = estimatedFare - percent;
       if (nome_adc_1.isChecked()) {
           estimatedFare = soma;
       } else {
           estimatedFare = subtrai;
       }
       tvEstimatedFare.setText(getNewNumberFormat(estimatedFare));
   }
});

The above code I use for a checkbox. When the checkbox is clicked, it takes the value of the estimatedFare and adds 20%. works perfectly. The problem is that when unchecked, the subtraction it makes is different, the value decreases by about 0.25 of the total value. That is, if you keep checking and unchecking the checkbox, the value shown goes to zero. someone knows the reason?

1 answer

-2


Hello, this is because you are getting 20% of the result.

Example:

Initial value of the estimatedFare is 100. You score it adds 20 which is 20% of 100. estimatedFare now is 120.

When you cancel, he’ll subtract 20% from 120, which is 24. The value goes back to 96.

In this scenario it is better to save the original value and work with it.

  • That’s exactly what’s happening! instead of subtracting the percentage of the estimatedFare it is subtracting the percentage of the estimatedFare after +=.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.