Add and remove percentage

0

On a certain screen I get a value and add 10% over that value.

100,00 + 10% = 110,00

I save the final value (110.00). I'm having a hard time getting the final value (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, however it is going wrong in some cases, like 220.00

Double ofertaNormal = proposta.getOferta() / (1+(10.0/100));
ofertaNormal = (double) Math.round(ofertaNormal);
    
asked by anonymous 08.03.2014 / 06:33

1 answer

3

10% of 110 gives 11, so the result is correct. To get 100, the question is "what value does 110 give when 10% is added?" That is:

1.1x = 110

Logo:

x = 110 / 1.1
x = 100

Edit: Your Java code looks correct. If you're having problems with broken numbers, it's because of an inherent precision problem with Double (see here for 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 .

    
08.03.2014 / 06:50