All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.core.util.NumberUtil Maven / Gradle / Ivy

The newest version!
package com.rt.core.util;

import org.apache.commons.lang3.math.NumberUtils;

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

/**
 * 数字计算类
 */
public class NumberUtil extends NumberUtils {

    /**
     * 格式化数字
     *
     * @param number number
     * @param format format
     * @return String
     */
    public static String format(BigDecimal number, String format) {
        if (number == null) {
            return null;
        }
        return new DecimalFormat(format).format(number.doubleValue());
    }

    /**
     * 格式化数字
     *
     * @param number number
     * @param format format
     * @return String
     */
    public static String format(double number, String format) {
        return format(BigDecimal.valueOf(number), format);
    }

    /**
     * 格式化数字
     *
     * @param number number
     * @param format format
     * @return String
     */
    public static String format(String number, String format) {
        return format(RTUtil.toBigDecimal(number), format);
    }

    /**
     * 格式化数值
     *
     * @param val val
     * @param newScale newScale
     * @return double
     */
    public static double format(double val, int newScale) {
        return format(val, newScale, BigDecimal.ROUND_UP);
    }

    /**
     * 格式化数值
     * BigDecimal.ROUND_DOWN 直接删除多余的小数位,如2.35会变成2.3
     * BigDecimal.ROUND_UP 进位处理,2.31变成2.4
     * BigDecimal.ROUND_HALF_UP 四舍五入,2.35变成2.4
     * BigDecimal.ROUND_HALF_DOWN 四舍五入,2.35变成2.3,如果是5则向下舍
     *
     * @param val val
     * @param newScale newScale
     * @param roundingMode roundingMode
     * @return double
     */
    public static double format(String val, int newScale, int roundingMode) {
        return new BigDecimal(RTUtil.val(val, 0)).setScale(newScale,
                roundingMode).doubleValue();
    }

    /**
     * 格式化数值
     * BigDecimal.ROUND_DOWN 直接删除多余的小数位,如2.35会变成2.3
     * BigDecimal.ROUND_UP 进位处理,2.35变成2.4
     * BigDecimal.ROUND_HALF_UP 四舍五入,2.35变成2.4
     * BigDecimal.ROUND_HALF_DOWN 四舍五入,2.35变成2.3,如果是5则向下舍
     *
     * @param val val
     * @param newScale newScale
     * @param roundingMode roundingMode
     * @return double
     */
    public static double format(double val, int newScale, int roundingMode) {
        return BigDecimal.valueOf(val).setScale(newScale, roundingMode)
                .doubleValue();
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend,
                                    BigDecimal defaultValue) {
        BigDecimal rs = divide(divisor, dividend);
        return rs == null ? defaultValue : rs;
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @param scale scale
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend,
                                    int scale, BigDecimal defaultValue) {
        BigDecimal rs = divide(divisor, dividend, scale);
        return rs == null ? defaultValue : rs;
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend) {
        if (divisor == null || dividend == null) {
            return null;
        }
        if (dividend.compareTo(BigDecimal.valueOf(0)) == 0) {
            return null;
        }
        return divisor.divide(dividend, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 除法 默认四舍五入
     *
     * @param divisor divisor
     * @param dividend dividend
     * @param scale scale
     * @return BigDecimal
     */
    public static BigDecimal divide(BigDecimal divisor, BigDecimal dividend,
                                    int scale) {
        if (divisor == null || dividend == null) {
            return null;
        }
        if (dividend.compareTo(BigDecimal.valueOf(0)) == 0) {
            return null;
        }
        return divisor.divide(dividend, scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 填充
     *
     * @param length length
     * @param defaultValue defaultValue
     * @return BigDecimal[]
     */
    public static BigDecimal[] fill(int length, BigDecimal defaultValue) {
        BigDecimal[] array = new BigDecimal[length];
        for (int i = 0; i < length; i++) {
            array[i] = defaultValue;
        }
        return array;
    }

    /**
     * 填充
     *
     * @param length length
     * @param defaultValue defaultValue
     * @return Integer[]
     */
    public static Integer[] fill(int length, Integer defaultValue) {
        Integer[] array = new Integer[length];
        for (int i = 0; i < length; i++) {
            array[i] = defaultValue;
        }
        return array;
    }

    /**
     * 求N个数的最大值
     *
     * @param array array
     * @return BigDecimal
     */
    public static Object max(Object[] array) {
        if (array == null) {
            return BigDecimal.valueOf(0);
        }
        double[] dArray = new double[array.length];
        for (int i = 0; i < array.length; i++) {
            if (array[i] == null) {
                dArray[i] = 0;
            }
            if (array[i] instanceof String && isNumber(array[i].toString())) {
                dArray[i] = Double.valueOf(array[i].toString()).doubleValue();
            } else if (array[i] instanceof BigDecimal) {
                dArray[i] = ((BigDecimal) array[i]).doubleValue();
            } else {
                dArray[i] = 0;
            }
        }
        double rs = max(dArray);
        for (int i = 0; i < dArray.length; i++) {
            if (rs == dArray[i]) {
                return array[i];
            }
        }
        return BigDecimal.valueOf(0);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy