如何获得给定十进制值的百分比
How to get a percentage in a given decimal value
我有一个名为 amountDue
的变量,它保存了一笔交易的总金额,另一个名为 discount
的变量,它保存了将从 amountDue
中减去的百分比折扣,以及结果存储在另一个名为 discountedAmount
.
的变量中
我的问题是如何计算以百分比形式给出的折扣并将其从到期金额中减去并将新值分配给折扣金额变量?
discountedAmount = amountDue - ((discount/100) * amountDue)
这真的是非常基础的数学。首先,通常最好将百分比存储为 0 到 1 之间的值,而不是 0 到 100(更容易计算)。
首先我们将折扣百分比除以 100 以获得介于 0 和 1 之间的值。然后我们将此数字乘以总金额以获得折扣的货币值。最后我们从正常价格中减去这个值,以折扣价结束。
因此,对于通常标价 200 美元且折扣 10% 的商品,我们得到:
10 / 100 = 0.1
0.1 * 200 = 20 (the discount in dollars)
200 - 20 = 180 (the discounted price)
这可能会解决您的问题
discountedAmount = amountDue * discount / 100;
例如
amountDue = Rs. 700
discount = 10%
discountedAmount = Rs. 70
Amount to be paid = Rs. 700 - 70 = Rs. 630
我有一个名为 amountDue
的变量,它保存了一笔交易的总金额,另一个名为 discount
的变量,它保存了将从 amountDue
中减去的百分比折扣,以及结果存储在另一个名为 discountedAmount
.
我的问题是如何计算以百分比形式给出的折扣并将其从到期金额中减去并将新值分配给折扣金额变量?
discountedAmount = amountDue - ((discount/100) * amountDue)
这真的是非常基础的数学。首先,通常最好将百分比存储为 0 到 1 之间的值,而不是 0 到 100(更容易计算)。
首先我们将折扣百分比除以 100 以获得介于 0 和 1 之间的值。然后我们将此数字乘以总金额以获得折扣的货币值。最后我们从正常价格中减去这个值,以折扣价结束。
因此,对于通常标价 200 美元且折扣 10% 的商品,我们得到:
10 / 100 = 0.1
0.1 * 200 = 20 (the discount in dollars)
200 - 20 = 180 (the discounted price)
这可能会解决您的问题
discountedAmount = amountDue * discount / 100;
例如
amountDue = Rs. 700
discount = 10%
discountedAmount = Rs. 70
Amount to be paid = Rs. 700 - 70 = Rs. 630