如何在JavaScript中计算含税的价格和数量

时间:2018-08-20 13:39:51

标签: javascript math calculator

我的价格:20

数量:2

税金:10%

如何计算?

我不知道进行此计算的公式。

3 个答案:

答案 0 :(得分:5)

您的情况:

XYZ

或更笼统:

var priceWithTax = 20 * 1.10 * 2;

因素的顺序无关紧要。

答案 1 :(得分:2)

只需将它们全部相乘:

const price = 20;
const quantity = 2;
const taxRate = 0.1;
const tax = price * quantity * taxRate
const totalPrice = price * quantity + tax;

答案 2 :(得分:1)

var price = 20;
var tax = 0.1;
var qty = 2;
//amount includes the tax
var amountWithTax = (qty * price) / (1 - 0.1);
console.log(amountWithTax);
//amount you get after you pay the tax
var amountAfterDeductTax = amountWithTax - (amountWithTax * tax) ;
console.log(amountAfterDeductTax)