package com.yd.csf.api.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class ExchangeUtils {
    private static final BigDecimal ONE_HUNDRED = new BigDecimal("100");
    private static final int SCALE = 4;
    private static final int DIVISION_SCALE = 10;

    /**
     * 计算目标金额
     * @param fromAmount 原币种金额（不能为 null）
     * @param percent 汇率百分比，例如 20.15 表示 20.15%（不能为 null，通常 >0）
     * @return 目标金额，保留四位小数，四舍五入
     */
    public static BigDecimal calculateTargetAmount(BigDecimal fromAmount, BigDecimal percent) {
        if (fromAmount == null || percent == null) {
            throw new IllegalArgumentException("fromAmount and percent must not be null");
        }
        BigDecimal rate = percent.divide(ONE_HUNDRED, DIVISION_SCALE, RoundingMode.HALF_UP);
        BigDecimal target = fromAmount.multiply(rate);
        return target.setScale(SCALE, RoundingMode.HALF_UP);
    }

    public static void main(String[] args) {
        System.out.println(calculateTargetAmount(new BigDecimal(20102.8921), new BigDecimal(20.18)));
    }
}