dimanche 1 mars 2015

What is the most appropriate way to calculate the successive discount of a price?



Example:



Price: 100$ | Discount: 10% | Successive discount: 5%



The successive discount applies to the price after the discount, so I'm doing something like this:



float price = 100f;
float disc = 10f;
float sucDisc = 5f;
float finalPrice = (price * (1f - (disc / 100))) * (1f - (sucDisc / 100));
System.out.println(finalPrice);


Output:



85.5


This seems to be correct, but this is a very old problem and I'm probably reinventing the wheel, so I'm wondering if there is a better way of doing this. By better I mean a method that improves readability, efficiency and/or accuracy.




Aucun commentaire:

Enregistrer un commentaire