简体   繁体   English

用Java计算交易费用

[英]Calculating transaction fees in Java

Lets say you have website that keeps balance for each user. 假设您拥有一个可以让每个用户保持平衡的网站。 You give each user option to deposit money into their balance from PayPal, Amazon FPS, Authorize.NET eCheck and Credit Card and maybe couple of there once. 您可以给每个用户选择从PayPal,Amazon FPS,Authorize.NET eCheck和信用卡(可能只有几次)中存入其余额的资金。

Each of the above companies charges fee. 上述每个公司都收费。 And since you are the receiver you are responsible for fees (except for Adaptive PayPal API there you can say who will pay the fees). 并且由于您是接收方,您需要负责费用(除了Adaptive PayPal API之外,您可以说谁将支付费用)。

So lets say website user (sender) will deposit you $1000.00 And lets picker fee of 2.9% plus $0.30 fix fee = -$29.30 So the receiver (the website owner) ends up with $970.70 因此,假设网站用户(发送者)将向您存入$ 1000.00,并让2.9%的选择器费用加上$ 0.30的固定费用=-$ 29.30,那么接收者(网站所有者)最终将获得$ 970.70

If you try to charge $1029.30 you end up with $999.1503 If you try to charge $1031.11 you end up with $1000.90781 which is acceptable (this is when the fee is increased by 0.181% to 3.081% + 0.30) 如果您尝试收取$ 1029.30,则最终会获得$ 999.1503如果您尝试收取$ 1031.11,则最终会导致获得$ 1000.90781,这是可以接受的(这是将费用增加0.181%至3.081%+ 0.30)

So this question is, what is the best way to calculate the 0.181% in Java method 所以这个问题是,用Java方法计算0.181%的最佳方法是什么

I know this is more of math problem than Java problem but what would be the best algorithm to use for guessing the best fee percentage so the final value would get as close as possible to the final value of what the website user is try to deposit. 我知道这比Java问题更多的是数学问题,但是什么是猜测最佳费用百分比的最佳算法,因此最终价值将尽可能接近网站用户尝试存入的最终价值。

So the method should take the actual fee 2.9% (0.029 for multiplication), the fix fee 0.30 the amount the user is trying to deposit and figure out the final sum to be charged that would be as close as possible to the deposit amount. 因此,该方法应采用实际费用的2.9%(乘以0.029),固定费用为用户尝试存入的金额的0.30,并确定最终收取的金额应尽可能接近存款金额。

The way I have been doing it is trying to increase the 2.9% until I hit slightly higher after the fees are subtracted. 我一直在尝试增加2.9%,直到扣除费用后我的税率略高。

UPDATE UPDATE

As per bellow answers I coded up this method. 根据下面的答案,我编写了此方法。 Any further suggestions are more than welcome: 我们欢迎任何进一步的建议:

public static float includeFees(float percentageFee, float fixedFee, float amount) {
    BigDecimal amountBD = new BigDecimal(amount);
    BigDecimal percentageFeeBD = new BigDecimal(1-percentageFee);
    BigDecimal fixedFeeBD = new BigDecimal(fixedFee);

    return amountBD.add(fixedFeeBD).divide(percentageFeeBD, 10, BigDecimal.ROUND_UP)
            .setScale(2, BigDecimal.ROUND_UP).floatValue();
}

Any time you're dealing with money you'll want to use a precision data type rather than floating point math. 每当您要花钱时,都需要使用精度数据类型而不是浮点数学。 In general there are rules about how you round on matters when dealing with things like sales tax. 通常,在处理诸如营业税之类的事情时,有关于如何处理问题的规则。

BigDecimal has parameters you can set to control the rounding behavior that will probably get you to where you need to go. BigDecimal具有可以设置为控制舍入行为的参数,这些舍入行为可能会将您带到需要去的地方。 In any case like this the math will never be exact on fractions of a cent but you should be able to guarantee that you'd never charge more than an extra fraction of a penny. 在这种情况下,数学永远不会精确到一分钱的几分之一,但是您应该能够保证您收取的费用永远不会超过一分钱。

amt_you_receive = amt_customer_pays - .029 * amt_customer_pays - .30
amt_you_receive + .30 = amt_customer_pays * (1 - .029)
(amt_you_receive + .30) / .971 = amt_customer_pays

So take the amount you want to get, add 30 cents, and divide by .971 (aka multiply by approximately 1.029866) to get the amount you should charge. 因此,取您要获得的金额,加上30美分,然后除以.971(也就是乘以约1.029866)即可获得您应该收取的金额。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM