Add and remove percentage

Asked

Viewed 879 times

0

On a certain screen I take a value and add 10% on top of that value.

100,00 + 10% = 110,00

I save the final amount (110.00). I’m having difficulty in picking up the final amount (110.00) and taking the 10%. 110.00 - 10% = 99.00 and in real I wanted to return to 100.00.

I found the following formula in java, but it is going wrong in some cases, as 220,00

Double ofertaNormal = proposta.getOferta() / (1+(10.0/100));
ofertaNormal = (double) Math.round(ofertaNormal);
  • my... 10% of 110 is 11 and not 10...

1 answer

3

10% of 110 is 11, so the result is correct. To get 100, the question is "what is 110 when you add 10%?":

1.1x = 110

Soon:

x = 110 / 1.1
x = 100

Edit: Your Java code looks right. If you are having problems with broken numbers, it is because of an inherent accuracy problem with Double (see here an explanation; the question is about Javascript, but the problem is the same in Java with Double and Float). In this case I believe you can use BigDecimal instead of Double.

  • The 220.00 is correct, but with broken numbers, for example 4.02 it is going wrong

  • Thank you very much, I’ll take a look

Browser other questions tagged

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