package com.yem.tws.common1;

import java.math.BigDecimal;
import java.text.NumberFormat;


/**
 * 金额工具类
 * @author 86173
 *
 */
public class BigDecimalUtils {

	public static BigDecimal zero = new BigDecimal("0.00");
	public static BigDecimal zero_point_six = new BigDecimal("0.6");
	public static BigDecimal one = new BigDecimal("1.00");
	public static BigDecimal one_handred = new BigDecimal("100");
	public static BigDecimal one_thousand = new BigDecimal("1000");
	public static BigDecimal one_million = new BigDecimal("1000000");
	public static BigDecimal one_handred_ten = new BigDecimal("110");
	public static BigDecimal ten_thousand = new BigDecimal("10000");
	
	/***
	 * 金额格式转换
	 * @param Object
	 * @return
	 */
	public static BigDecimal toBigDecimal(Object obj)
    {
      if(obj == null)
           return BigDecimalUtils.zero;
        if(obj instanceof BigDecimal)
            return (BigDecimal)obj;
        if(obj instanceof Integer)
            return new BigDecimal(((Integer)obj).toString());
        if(obj instanceof Long)
            return new BigDecimal(((Long)obj).toString());
        if(obj instanceof Double)
            return new BigDecimal(((Double)obj).doubleValue());
        if(obj.toString() == null)
            return BigDecimalUtils.zero;
        String str = obj.toString().trim();
        if(str.toLowerCase().indexOf("e") > -1)
            try
            {
                return new BigDecimal(str);
            }
            catch(NumberFormatException e)
            {
               return BigDecimalUtils.zero;
            }
        if(str.matches("^[+-]?\\d+[\\.\\d]?\\d*+$"))
            return new BigDecimal(str);
        else
            return BigDecimalUtils.zero;
    }
	
	/***
	 * 除
	 * @param  Object
	 * @param  Object
	 * @param scale 精度
	 * @return 金额
	 */
	public static BigDecimal div(Object a, Object b,int scale) {
		if (a == null)
			a = zero;
		if (b == null)
			return zero;

		if (a instanceof BigDecimal && b instanceof BigDecimal) {
			BigDecimal aVal = (BigDecimal) a;
			BigDecimal bVal = (BigDecimal) b;
			return aVal.divide(bVal,scale,BigDecimal.ROUND_HALF_UP);
		}

		return zero;
	}
	/***
	 * 除
	 * @param BigDecimal
	 * @param BigDecimal
	 * @param scale 精度
	 * @return 金额
	 */
	public static BigDecimal div(BigDecimal aVal, BigDecimal bVal,int scale) {

		if (aVal == null)
			aVal = zero;

		if (bVal == null)
			return zero;

		if (bVal.compareTo(zero) == 0)
			return zero;
		BigDecimal ret = aVal.divide(bVal, scale, BigDecimal.ROUND_HALF_UP);

		return ret;

	}
	/***
	 * 除
	 * @param BigDecimal
	 * @param BigDecimal
	 * @return 整数
	 */
	public static BigDecimal getIntDiv(BigDecimal aVal, BigDecimal bVal) {

		if (aVal == null)
			aVal = zero;

		if (bVal == null)
			return zero;

		if (bVal.compareTo(zero) == 0)
			return zero;
		BigDecimal ret = aVal.divide(bVal, 0, BigDecimal.ROUND_UP);

		return ret;

	}
	/***
	 * 乘
	 * @param Object
	 * @param Object
	 * @param scale
	 * @return
	 */
	public static BigDecimal multiply(Object a, Object b,int scale) {

		if (a == null)
			a = zero;

		if (b == null)
			b = zero;

		if (a instanceof BigDecimal && b instanceof BigDecimal) {
			BigDecimal aVal = (BigDecimal) a;
			BigDecimal bVal = (BigDecimal) b;
			return aVal.multiply(bVal).setScale(scale,BigDecimal.ROUND_HALF_UP);
		}

		return zero;
	}
	/***
	 * 乘
	 * @param BigDecimal
	 * @param BigDecimal
	 * @param scale
	 * @return
	 */
	public static BigDecimal multiply(BigDecimal aVal, BigDecimal bVal,int scale) {
		if (aVal == null)
			aVal = zero;

		if (bVal == null)
			bVal = zero;

		BigDecimal ret = aVal.multiply(bVal).setScale(scale,BigDecimal.ROUND_HALF_UP);

		return ret;

	}
	
	/***
	 * 加
	 * @param Object
	 * @param Object
	 * @param scale
	 * @return
	 */
	public static BigDecimal add(Object a, Object b,int scale) {

		if (a == null)
			a = zero;

		if (b == null)
			b = zero;

		if (a instanceof BigDecimal && b instanceof BigDecimal) {
			BigDecimal aVal = (BigDecimal) a;
			BigDecimal bVal = (BigDecimal) b;
			return aVal.add(bVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
		}

		return zero;
	}
	/***
	 * 加
	 * @param BigDecimal
	 * @param BigDecimal
	 * @param scale
	 * @return
	 */
	public static BigDecimal add(BigDecimal aVal, BigDecimal bVal,int scale) {

		if (aVal == null)
			aVal = zero;

		if (bVal == null)
			bVal = zero;

		BigDecimal ret = aVal.add(bVal);
		ret = ret.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return ret;

	}
	
	/***
	 * 减
	 * @param Object
	 * @param Object
	 * @param scale
	 * @return
	 */
	public static BigDecimal sub(Object a, Object b,int scale) {

		if (a == null)
			a = zero;

		if (b == null)
			b = zero;

		if (a instanceof BigDecimal && b instanceof BigDecimal) {
			BigDecimal aVal = (BigDecimal) a;
			BigDecimal bVal = (BigDecimal) b;
			return aVal.subtract(bVal).setScale(scale, BigDecimal.ROUND_HALF_UP);
		}

		return zero;
	}
	/***
	 * 减
	 * @param BigDecimal
	 * @param BigDecimal
	 * @param scale
	 * @return
	 */
	public static BigDecimal sub(BigDecimal aVal, BigDecimal bVal,int scale) {

		if (aVal == null)
			aVal = zero;

		if (bVal == null)
			bVal = zero;

		BigDecimal ret = aVal.subtract(bVal);
		ret = ret.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return ret;

	}
	
	// 加1
	public static BigDecimal addOne(Object a,int scale) {
		return add(a, one,scale);
	}

	// 减1
	public static BigDecimal subOne(Object a,int scale) {
		return sub(a, one,scale);
	}
	
	/**
	 * 金额转大写
	 * @param n
	 * @return
	 */
	public static String digitCapital(double n) {
	    String fraction[] = {"角", "分"};
	    String digit[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
	    String unit[][] = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};

	    String head = n < 0 ? "负" : "";
	    // 如果是负数取绝对值
	    n = Math.abs(n);
	    String s = "";
	    BigDecimal bigDecimal = new BigDecimal(Double.valueOf(n).toString());
	    String nStr = bigDecimal.toPlainString();
	    // 小数部分
	    String[] split = nStr.split("\\.");
	    if (split.length > 1) {
	        // 小数点为特殊符号，在分割时需进行转义
	        String decimalStr = split[1];
	        if (decimalStr.length() > 2) {
	            decimalStr = decimalStr.substring(0, 2);
	        }
	        // 将小数部分转换为整数
	        Integer integer = Integer.valueOf(decimalStr);
	        String p = "";
	        for (int i = 0; i < decimalStr.length() && i < fraction.length; i++) {
	            p = digit[integer % 10] + fraction[decimalStr.length() - i - 1] + p;
	            integer = integer / 10;
	        }
	        s = p.replaceAll("(零.)+", "") + s;
	    }
	    if (s.length() < 1) {
	        s = "整";
	    }
	    int integerPart = (int)Math.floor(n);
	    // 整数部分
	    for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
	        String p = "";
	        for (int j = 0; j < unit[1].length && n > 0; j++) {
	            p = digit[integerPart % 10] + unit[1][j] + p;
	            integerPart = integerPart / 10;
	        }
	        s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
	    }
	    return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
	}
	
	/*
	 *向上取整
	 */
	public static BigDecimal ceil(double aVal,int scale) {
		aVal = Math.ceil(aVal);
		BigDecimal aVal1=BigDecimal.valueOf(aVal);
		aVal1 = aVal1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return aVal1;
	}
	
	/*
	 * 向下取整
	 */
	public static BigDecimal floor(double aVal,int scale) {
		aVal = Math.floor(aVal);
		BigDecimal aVal1=BigDecimal.valueOf(aVal);
		aVal1 = aVal1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return aVal1;
	}
	
	/*
	 * 四舍五入
	 */
	public static BigDecimal toBigDecimal(Object obj,int scale) {

		BigDecimal aVal1 =toBigDecimal(obj);
		aVal1 = aVal1.setScale(scale, BigDecimal.ROUND_HALF_UP);
		return aVal1;
	}
	
	public static String formatDouble(Object d) {
        NumberFormat nf = NumberFormat.getInstance();
        //设置保留多少位小数
        nf.setMaximumFractionDigits(20);
         // 取消科学计数法
        nf.setGroupingUsed(false);
        //返回结果
        return nf.format(d);
    }
}
