package com.desfate.chart.util;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
/**
 * Created by cxl on 2018/4/11.
 */

public class MathUtil {

    /**
     * 四舍五入保留小数点后两位
     *
     * @param string
     * @param num    预留小数位
     * @return
     */
    public static String roundNum(String string, int num) {
        if ("".equals(string)) {
            return string;
        }
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        return decimalFormat.format(Double.parseDouble(string));
    }

    /**
     * 四舍五入保留小数点后两位
     *
     * @param value
     * @param num   预留小数位
     * @return
     */
    public static String roundNum(double value, int num) {
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        return decimalFormat.format(value);
    }

    // FIXME: 2018/10/20  要加个方法  2位以内 不限制  4位再限制


    /**
     * 四舍五入保留小数点后两位
     *
     * @param string
     * @param num    预留小数位
     * @return
     */
    public static String roundNoNum(String string, int num) {
        if ("".equals(string)) {
            return string;
        }
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        return decimalFormat.format(Double.parseDouble(string));
    }

    /**
     * 判断字符串是否为大于0 的有效数字
     *
     * @param number
     * @return
     */
    public static boolean isZeroOrNull(String number) {
        if (number == null || number.equals("") || number.equals("0")) {
            return true;
        }

        double temp = 0;
        try {
            temp = Double.parseDouble(number);
        } catch (Exception e) {
            // TODO: handle exception
        }

        return !(temp > 0);
    }


    public static boolean isEmpty(String str) {
        return !(str == null || str.equals("") || str.equals("null"));
    }

    /**
     * 向上取整
     *
     * @param value
     * @param num
     * @return
     */
    public static String roundNumDown(double value, int num) {
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        return decimalFormat.format(value);
    }

    /**
     * 向上取整
     *
     * @param string
     * @param num    预留小数位
     * @return
     */
    public static String roundNumDown(String string, int num) {
        if ("".equals(string)) {
            return string;
        }
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        decimalFormat.setRoundingMode(RoundingMode.DOWN);
        return decimalFormat.format(Double.parseDouble(string));
    }


    /**
     * 向上取整
     *
     * @param value
     * @param num
     * @return
     */
    public static String roundNumUP(double value, int num) {
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        decimalFormat.setRoundingMode(RoundingMode.UP);
        return decimalFormat.format(value);
    }

    /**
     * 向上取整
     *
     * @param string
     * @param num    预留小数位
     * @return
     */
    public static String roundNumUP(String string, int num) {
        if ("".equals(string)) {
            return string;
        }
        String s = "0";
        switch (num) {
            case 0:
                s = "0";
                break;
            case 1:
                s = "0.0";
                break;
            case 2:
                s = "0.00";
                break;
            case 3:
                s = "0.000";
                break;
            case 4:
                s = "0.0000";
                break;
            case 5:
                s = "0.00000";
                break;
            default:
                s = "0.00000";
                break;
        }
        DecimalFormat decimalFormat = new DecimalFormat(s);
        decimalFormat.setRoundingMode(RoundingMode.UP);
        return decimalFormat.format(Double.parseDouble(string));
    }




    /**
     * 获取数据有几位小数
     *
     * @param price
     * @return
     */
    public static int getPointNum(double price) {
        String myPrice = String.valueOf(price);
        String[] split = myPrice.split("\\.");
        if (split.length == 2) {
            return split[1].length();
        } else {
            return 0;
        }
    }

    public static int getPointNum(String price) {
        String myPrice = price;
        String[] split = myPrice.split("\\.");
        if (split.length == 2) {
            return split[1].length();
        } else {
            return 0;
        }
    }

    /**
     * 获取有几位整数
     *
     * @param price
     * @return
     */
    public static int getIntegerNum(String price) {
        String myPrice = price;
        String[] split = myPrice.split("\\.");
        if (split.length == 2) {
            return split[0].length();
        } else {
            return split[0].length();
        }
    }



    //去除尾部不需要的0
    public static String removeBackZero(String num) {
        String[] split = num.split("\\.");
        if (split.length == 2) {
            if (split[1].equals("0")) {
                return split[0];
            } else if (split[1].equals("00")) {
                return split[0];
            } else if (split[1].equals("000")) {
                return split[0];
            } else if (split[1].equals("0000")) {
                return split[0];
            } else {
                return num;
            }
        } else {
            return num;
        }
    }


    public static String doubleToString(double info) {
        DecimalFormat decimalFormat = new DecimalFormat("#,##0.00");//格式化设置
        return decimalFormat.format(info);
    }

    /**
     * Double 转string 去除科学记数法显示
     *
     * @param d
     * @return
     */
    public static String double2Str(Double d) {
        if (d == null) {
            return "";
        }
        NumberFormat nf = NumberFormat.getInstance();
        nf.setGroupingUsed(false);
        return (nf.format(d));
    }

    //判断是否是数字
    public static boolean isNumeric(String str)
    {
        if(str == null || str.length() == 0){
            return false;
        }
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
            return false;
            }
        }
        return true;
    }

    /**
     * 校验 是否是double
     * @param str
     * @return
     */
    public static boolean isDouble(String str){
        if(str == null || str.length() == 0){
            return false;
        }
        try{
            Double.valueOf(str);
            return true;
        }catch (NumberFormatException e){
            return false;
        }
    }
}
