mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-05-06 06:00:44 +08:00
20 lines
714 B
Go
20 lines
714 B
Go
|
|
package payment
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/shopspring/decimal"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CalculatePayAmount computes the total pay amount given a recharge amount and
|
||
|
|
// fee rate (percentage). Fee = amount * feeRate / 100, rounded UP (away from zero)
|
||
|
|
// to 2 decimal places. The returned string is formatted to exactly 2 decimal places.
|
||
|
|
// If feeRate <= 0, the amount is returned as-is (formatted to 2 decimal places).
|
||
|
|
func CalculatePayAmount(rechargeAmount float64, feeRate float64) string {
|
||
|
|
amount := decimal.NewFromFloat(rechargeAmount)
|
||
|
|
if feeRate <= 0 {
|
||
|
|
return amount.StringFixed(2)
|
||
|
|
}
|
||
|
|
rate := decimal.NewFromFloat(feeRate)
|
||
|
|
fee := amount.Mul(rate).Div(decimal.NewFromInt(100)).RoundUp(2)
|
||
|
|
return amount.Add(fee).StringFixed(2)
|
||
|
|
}
|