package com.ywt.outpatient.core.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;

public final class MoneyUtil {
    private MoneyUtil() {
        throw new UnsupportedOperationException("U can't instantiate me...");
    }

    /**
     * 元转分，double to int某些情况下会丢失精度，所以用BigDecimal.
     *
     * @param number
     * @return
     */
    public static int yuan2Cent(double number) {
        BigDecimal v1 = new BigDecimal(String.valueOf(number));
        BigDecimal v2 = new BigDecimal("100");
        BigDecimal result = v1.multiply(v2);
        return result.intValue();
    }

    /**
     * 元转分，BigDecimal to int
     *
     * @param number
     * @return
     */
    public static int yuan2Cent(BigDecimal number) {
        if (number == null) return 0;
        BigDecimal v2 = new BigDecimal("100");
        BigDecimal result = number.multiply(v2);
        return result.intValue();
    }


    /**
     * 分转元，int to str某些情况下会丢失精度，所以用BigDecimal.
     *
     * @param number
     * @return
     */
    public static String cent2Yuan(int number) {
        if (number == 0) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(new BigDecimal(number).divide(new BigDecimal("100")).doubleValue());
    }

    /**
     * 分转元，Long to str某些情况下会丢失精度，所以用BigDecimal.
     *
     * @param number
     * @return
     */
    public static String cent2Yuan(Long number) {
        if (number == 0L) {
            return "0.00";
        }
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(new BigDecimal(number).divide(new BigDecimal("100")).doubleValue());
    }


}
