package com.yunyan.common.utils;

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

public class NumberFormatUtils {

    public static void main(String[] args) {
        //String res = NumberFormatUtils.amountInThousands("1324678881122.331234567");
        //String res = NumberFormatUtils.addThousandSeparatorForInteger("0");
        //String res = NumberFormatUtils.addThousandSeparatorForInteger("0.20");
        String res = NumberFormatUtils.format1("2.20");
        //String res = NumberFormatUtils.addThousandSeparatorForInteger(BigDecimal.ZERO);
        System.out.println(res);
    }



    /**
     * 元转万元 用DecimalFormat来实现千分位的自适应输出
     */
    public static String amountInThousands(String strVal) {
        if (StringUtils.isBlank(strVal)) {
            return "";
        }

        BigDecimal valDecimal = new BigDecimal(strVal).divide(new BigDecimal(10000), 2, RoundingMode.HALF_DOWN);
        return addThousandSeparatorForInteger(valDecimal);

        /*int index = strVal.indexOf(".");
        if (index > 0) {
            String integerPartial = strVal.substring(0, index);
            String decimalPartial = strVal.substring(index);
            return addThousandSeparatorForInteger(integerPartial) + decimalPartial;

            switch (strVal.length() - (strVal.indexOf(".") + 1)) {
                case 0:
                    df = new DecimalFormat("###,##0");
                    break;
                case 1:
                    df = new DecimalFormat("###,##0.0");
                    break;
                case 2:
                    df = new DecimalFormat("###,##0.00");
                    break;
                case 3:
                    df = new DecimalFormat("###,##0.000");
                    break;
                case 4:
                    df = new DecimalFormat("###,##0.0000");
                    break;
                default:
                    df = new DecimalFormat("###,##0.00000");
                    break;
            }
        } else {
            return addThousandSeparatorForInteger(strVal);
        }*/
    }

    /**
     * 万元 转元 用DecimalFormat来实现千分位的自适应输出
     * @param strVal
     * @return
     */
    public static String amountInThousandsYuan(String strVal) {
        if (StringUtils.isBlank(strVal)) {
            return "";
        }

        BigDecimal valDecimal = new BigDecimal(strVal).multiply(new BigDecimal(10000));
        return addThousandSeparatorForInteger(valDecimal);
    }

    /**
     *
     * @param strVal
     * @param pattern "###,##0"
     * @return
     */
    public static String addThousandSeparator(String strVal, String pattern) {
        if (StringUtils.isBlank(strVal)) {
            return "";
        }

        DecimalFormat df = new DecimalFormat(pattern);
        String strResult = df.format(new BigDecimal(strVal));
        return strResult;
    }

    /**
     * 只给整数加千分位分隔符, 0结尾的不显示
     * @param strVal
     * @return
     */
    public static String addThousandSeparatorForInteger(String strVal) {
        //DecimalFormat df = new DecimalFormat("###,##0.00");
        DecimalFormat df = new DecimalFormat("###,##0.##");
        String strResult = df.format(new BigDecimal(strVal));
        return strResult;
    }

    /**
     * 0结尾的不显示
     * @param decimal
     * @return
     */
    public static String addThousandSeparatorForInteger(BigDecimal decimal) {
        //DecimalFormat df = new DecimalFormat("###,##0.00");
        DecimalFormat df = new DecimalFormat("###,##0.##");
        // 设置千位分隔符
        DecimalFormatSymbols symbols = new DecimalFormatSymbols();
        symbols.setGroupingSeparator(',');
        String strResult = df.format(decimal);

        return strResult;
    }

    /**
     * 格式化 1位小数, 0结尾不显示
     * @param strVal
     * @return
     */
    public static String format1(String strVal) {
        String strResult = format1(new BigDecimal(strVal));
        return strResult;
    }

    /**
     * 格式化 1位小数, 0结尾不显示
     * @param decimal
     * @return
     */
    public static String format1(BigDecimal decimal) {
        DecimalFormat df = new DecimalFormat("#.#");
        String strResult = df.format(decimal);
        return strResult;
    }

}
