I have to add up the service charge(10%) plus VAT(8%) to a service, and I'm not sure the right way to increase a number by percentages.
For example: The service costs $1000 and I need to add to it 10% plus 8%. I'm struggling with three different ways to achieve that. Which way is the right one?
var service_charge = 10; // percent
var vat = 8; // percent
var price = 1000;
// OPTION 1
var tmp1 = price * (service_charge / 100);
var tmp2 = price * (vat / 100);
var total_1 = price + tmp1 + tmp2;
// OPTION 2
var tmp3 = price * (service_charge / 100);
var sub_total = price + tmp3;
var tmp4 = sub_total * (vat / 100);
var total_2 = sub_total + tmp4;
// OPTION 3
var tmp5 = price * ((service_charge + vat) / 100);
var total_3 = price + tmp5;
console.log(total_1);
console.log(total_2);
console.log(total_3);
https://jsbin.com/povama/edit?js,console
I think "option 2" is the right one, but I really want to be sure.
If VAT is to be applied after service charge, option 2 is the good one. If you want to summarize it in one single line it would be something like this:
var total = (price*(1+(service_charge/100)))*(1+(vat/100));