Javascript 我如何计算固定数字的 PayPal 费用 (2.9% + .30)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10613385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I calculate PayPal's fees (2.9% + .30) on a fixed number?
提问by WebMW
Sounds like an easy question, I know. I am literally stumped.
听起来是个简单的问题,我知道。我真的被难住了。
Here are two websites that have made the simple calculator:
这里有两个网站制作了简单的计算器:
I'm needing to calculate these PayPal's fees in my PHP application.
我需要在我的 PHP 应用程序中计算这些 PayPal 的费用。
Here's what I got, say we wanted to be paid $30 "even" for membership fees. This is the function I'm using to calculate what we should charge to make up for it using PayPal's calculations (also described on both of the sites above): 2.9% of the transaction + $0.30.
这就是我得到的,假设我们希望“偶数”支付会员费 30 美元。这是我用来计算我们应该使用 PayPal 的计算(也在上述两个网站上都有描述)来弥补它的费用的函数:交易的 2.9% + 0.30 美元。
function memfees()
{
$calc = round((30 * .029) + 30 + .30,2);
return $calc;
}
Test it here: http://matt.mw/junk/php/memfees.phpAs you can see, it says 31.17. Both of the other websites say it should be 31.20. If I try $130.00, mine calculates to $134.07 and both of theirs calculate to $134.19.
在这里测试:http: //matt.mw/junk/php/memfees.php如您所见,它表示 31.17。其他两个网站都说它应该是 31.20。如果我尝试 130.00 美元,我的计算为 134.07 美元,他们两个的计算为 134.19 美元。
What am I doing wrong?
我究竟做错了什么?
Why am I using a Round function? Percentages cause some decimals to go past the hundredths place, and PayPal generates an error if there is more then 2 digits after the decimal. I thought people normally round money, (e.g. $31.6342 will be $31.63) but if that is the case, what am I doing wrong in my function? The amount that is off is further if there are large payments. This leads me to believe that something is wrong with the percentage portion.
为什么我使用 Round 函数?百分比会导致一些小数点超过百分之一,如果小数点后超过 2 位,PayPal 会产生错误。我认为人们通常会圆钱,(例如,31.6342 美元将是 31.63 美元)但如果是这种情况,我的功能做错了什么?如果有大笔付款,则减少的金额会更多。这让我相信百分比部分有问题。
Could you offer some help on this?
你能提供一些帮助吗?
Thank you for your time.
感谢您的时间。
回答by Jordan
Your function does seem strange. To break it down, PayPal is charging a fixed rate of $.30, and adding a transaction percentage fee of 2.9%.
你的功能看起来很奇怪。详细来说,PayPal 收取 0.30 美元的固定费率,并增加 2.9% 的交易百分比费用。
The formula for this is to add the $.30 and then divide by the percentage difference (100% - 2.9%), which will give you the amount prior to the actual reduction by PayPal.
计算公式是将 0.30 美元相加,然后除以百分比差异 (100% - 2.9%),这将为您提供 PayPal 实际减少之前的金额。
function memfees($amount)
{
$amount += .30;
return $amount / (1 - .029);
}
You can round and float that as you wish.
您可以根据需要对其进行圆形和浮动。
回答by Gareth
Because you're thinking about it the wrong way around.
因为你想错了。
You don't charge $30, you charge $31.20. Paypal takes the transaction fee (2.9%) off of thatand then takes $0.30 from the result:
您不收取 30 美元,而是收取 31.20 美元。贝宝需要的交易费(2.9%)关闭的那个,然后从结果取$ 0.30:1
.20
* 0.971 (1 - 2.9%)
------
.2952
- y = x - x * .029 - .3
.30
------
.9952
So you have to reverse this process, i.e. add $0.3 to your total and divide by 0.971.
所以你必须颠倒这个过程,即在你的总数中增加 0.3 美元并除以 0.971。
回答by Sam
Here's a good mathematical explanation of how this works. We have this number, x, which we want to charge a credit card. We don't know what this number is, but we know that when we subtract 30 cents and subtract 2.9% of x, we get y, which is the amount of money we take home:
这是有关其工作原理的很好的数学解释。我们有这个数字 x,我们要从信用卡中扣款。我们不知道这个数字是什么,但我们知道当我们减去 30 美分并减去 x 的 2.9% 时,我们得到 y,这是我们带回家的钱数:
y = x - x * .029 - .3
y + .3 = x - x * .029
= x(1 - .029)
(y + .3) / (1 - .029) = x
x = (y + .3) / .971
We know y, because we know what amount we want to take home. Say, we wanted to take home $100, then y = 100. But what's x?
我们知道 y,因为我们知道我们想要带回家的金额。比如说,我们想带回家 100 美元,那么 y = 100。但是 x 是多少?
x = (y + .3) / .971
Note: because x - x * .029
can be written as x * 1 - x * .029
then all need to do is just use distributive property and we come up with x(1 - .029)
注意:因为x - x * .029
可以写成x * 1 - x * .029
那么所有需要做的就是使用分配属性,我们想出了x(1 - .029)
So we come up with the formula:
于是我们得出了一个公式:
x = (100 + .3) / .971
x = ~103.30
Which defines this infamous number, x. Also, it answers our question; What amount should I charge a card in order to cover the transaction fee and not fall short of the amount we want to take home? Well, all we need to do is fill in the take home amount, which is y:
它定义了这个臭名昭著的数字 x。此外,它回答了我们的问题;为了支付交易费用并且不低于我们想要带回家的金额,我应该从卡中收取多少费用?好吧,我们需要做的就是填写带回家的金额,即 y:
public decimal CalcFee()
{
//PayPal Percentage Fee = 0.29%, Fix Fee = 0.3
var sum = this.Items.Sum(c => c.Price * c.Qty); //Total Items
return Math.Round(Math.Round((sum + (decimal)0.3) / (decimal)0.971, 2) - sum, 2);
}
I hope this helps clarify.
我希望这有助于澄清。
回答by user8875455
In C#
在 C# 中
97.10 % = $ 30
1.00 % = $ 30 / 97.1
100.00 % = $ 30 / 97.1 * 100
============================
100.00 % ~ 30,90
回答by Christian Gollhardt
A math problem. 100 % is not what you think:
一道数学题。100% 不是你想的那样:
- 100 % + the fixed fee needs to be charged
- 100 % is actualy
97,1 % payout ($ 30) + 2,9 % fee
- To get 100 %, we apply the rule of three
- 100 % + 需要收取的固定费用
- 100 % 实际上是
97,1 % payout ($ 30) + 2,9 % fee
- 为了获得 100%,我们应用三法则
e.g.:
例如:
$ 30,90 + $ 0.30
Now we need to add the fixed fee:
现在我们需要添加固定费用:
//untested, not a php developer anymore
function requiredChargement($requiredPrice)
{
$requiredPercentage = 100.0 - 2.9;
$fullVariableCost = $requiredPrice / requiredPercentage * 100.0;
return round($fullVariableCost + 0.3, 2);
}
Which is resulting in the requested $ 31.20
这导致请求的 $ 31.20
So what does this mean in Code?
那么这在代码中意味着什么?
##代码##