package com.kfzs.duanduan.utils;

import android.text.TextUtils;

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

/**
 *
 * Created by HooRang on 2017/3/21.
 */
public class NumberFormatUtils {

    private static final int DIVISOR_DEFAULT = 10 ;

    public static float getDivideResult(int dividend){
        if (dividend <= 0){
            return 0.0f;
        }

        BigDecimal bDivisor = new BigDecimal(Double.valueOf(dividend));
        BigDecimal bDividend = new BigDecimal(Double.valueOf(DIVISOR_DEFAULT));
        BigDecimal result = bDivisor.divide(bDividend, 1,BigDecimal.ROUND_HALF_DOWN);

        return result.floatValue();
    }

    public static float getDivideResult(int dividend, int divisor){
        if (dividend <= 0){
            return 0.0f;
        }

        if (divisor > dividend){
            return 0.0f;
        }
        BigDecimal bDivisor = new BigDecimal(Double.valueOf(dividend));
        BigDecimal bDividend = new BigDecimal(Double.valueOf(divisor));
        BigDecimal result = bDivisor.divide(bDividend, 1,BigDecimal.ROUND_HALF_DOWN);

        return result.floatValue();
    }

    public static double decimal(double val, int newScale){
        BigDecimal   b   =   new   BigDecimal(val);
        double   f1   =   b.setScale(newScale,   BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    public static float parseFloat(String balance) {
        return parseFloat(balance, 0f);
    }

    public static float parseFloat(String numStr, float defaultValue) {
        if(!TextUtils.isEmpty(numStr)){
            try {
                return Float.parseFloat(numStr);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return defaultValue;
    }

    public static int parseInteger(String numStr) {
        return parseInteger(numStr, 0);
    }

    public static int parseInteger(String numStr, int defaultValue) {
        if(!TextUtils.isEmpty(numStr)){
            try {
                return Integer.parseInt(numStr);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return defaultValue;
    }
    public static long parseLong(String numStr) {
        return parseLong(numStr, 0);
    }

    public static long parseLong(String numStr, int defaultValue) {
        if(!TextUtils.isEmpty(numStr)){
            try {
                return Long.parseLong(numStr);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
        return defaultValue;
    }

    /**
     * 最多保留一位小数
     * @param bonus
     * @return
     */
    public static String retainMost1(double bonus) {
        DecimalFormat df = new DecimalFormat("#.#");
        return df.format(bonus);
    }
    /**
     * 最多保留两位小数
     * @param bonus
     * @return
     */
    public static String retainMost2(double bonus) {
        DecimalFormat df = new DecimalFormat("#.##");
        return df.format(bonus);
    }
    /**
     * 保留两位小数
     * @param bonus
     * @return
     */
    public static String retain2(float bonus) {
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(bonus);
    }

    //获取次数
    public static String getText(int no) {
        if(no > 10000)
            return String.format(Locale.CHINA, "%d万", no / 10000);
        return String.valueOf(no);
    }
}
