using System; namespace JiaZhiQuan.Common.Utils { public static class AmountUtils { /// /// 转换 元 -> 分 /// /// /// public static int ConvertYuanToCent(decimal amount) { return (int)(amount * 100); } /// /// 百分比转小数 /// /// /// public static decimal ConvertPercentToDeciaml(decimal d) { return d / 100; } /// /// 小数转百分比(保留两位小数) /// /// /// public static string ConvertDeciamlToPercent(decimal d) { return $"{AmountUtils.ConvertRoundReserveTwoDecimal(d * 100).ToString("0.00")}%"; } /// /// 分转元(不会牵扯到四舍五入) /// /// /// public static decimal ConvertCentToYuan(decimal cent) { return cent / 100m; } /// /// 分转元(保留两位小数, 不会牵扯到四舍五入) /// /// /// public static string ConvertCentToYuanStr(decimal cent) { return ConvertCentToYuan(cent).ToString("0.00"); } /// /// 四舍五入,保留两位小数 /// /// /// public static decimal ConvertRoundReserveTwoDecimal(decimal d) { return Math.Round(d, 2, MidpointRounding.AwayFromZero); } /// /// 四舍五入,取整 /// /// /// public static int ConvertRoundReserveRound(decimal d) { return (int)Math.Round(d, 0, MidpointRounding.AwayFromZero); } /// /// 计算税费(四舍五入取整) /// /// 总金额(分) /// 税率 /// public static int ComputeTaxAmount(int amount, decimal taxRate) { // (四舍五入保留整数)总税额 = 总金额(分) * 税率 return AmountUtils.ConvertRoundReserveRound(amount * taxRate); } /// /// 计算服务费(四舍五入取整) /// /// 总金额(分) /// 服务费率 /// public static int ComputeFeeAmount(int amount, decimal feeRate) { // (四舍五入保留整数)总服务费 = 总金额(分) * 服务费率 return AmountUtils.ConvertRoundReserveRound(amount * feeRate); } /// /// 计算真实金额 /// /// 总金额(分) /// 税率 /// 服务费率 /// 服务费最低金额(分) /// public static int ComputeRealAmount(int amount, decimal taxRate, decimal feeRate, int feeMinAmount) { // 去税总金额(分) = 总金额(分) - 税费 int amount1 = amount - ComputeTaxAmount(amount, taxRate); // 打款总金额(分) = 去税总金额(分) - 服务费(服务费有最低限制,如果计算的服务费小于最低限制,那么使用最低限制) int amount2 = amount1 - Math.Max(ComputeFeeAmount(amount1, feeRate), feeMinAmount); return amount2; } } }