BigDecimal计算的工具类

package utils;

import java.math.BigDecimal;

/**
 * 〈BigDecimal计算的小工具类〉
 *
 * @author Barrett
 * @version 1.0.0
 * @time 2019/12/16
 */
public class ArithUtil {
     

    /**
     * 由于Java的基本类型不能够对浮点数进行精确的运算,这个工具类提供精
     * 确的浮点数运算,包括加减乘除和四舍五入。
     */
    //默认除法运算精度
    private static final int DEF_DIV_SCALE = 10;

    //这个类不能实例化
    private ArithUtil() {
     
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 两个参数的和v1+v2
     */
    public static double add(double v1, double v2) {
     
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 两个参数的差:v1-v2
     */
    public static double sub(double v1, double v2) {
     
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 两个参数的积:v1*v2
     */
    public static double mul(double v1, double v2) {
     
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 两个参数的商:v1/v2
     */
    public static double div(double v1, double v2) {
     
        return div(v1, v2, DEF_DIV_SCALE);
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 两个参数的商:v1/v2 提供精确度数
     */
    public static double div(double v1, double v2, int scale) {
     
        if (scale < 0) {
     
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 四舍五入保留N位小数后的结果
     */
    public static double round(double v, int scale) {
     
        if (scale < 0) {
     
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(Double.toString(v));
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description double转成float
     */
    public static float convertsToFloat(double v) {
     
        BigDecimal b = new BigDecimal(v);
        return b.floatValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description double转成int
     */
    public static int convertsToInt(double v) {
     
        BigDecimal b = new BigDecimal(v);
        return b.intValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description double转成long
     */
    public static long convertsToLong(double v) {
     
        BigDecimal b = new BigDecimal(v);
        return b.longValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 比较返回两者中最大值
     */
    public static double returnMax(double v1, double v2) {
     
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.max(b2).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 比较返回两者中最小值
     */
    public static double returnMin(double v1, double v2) {
     
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.min(b2).doubleValue();
    }

    /**
     * @Author Barrett
     * @Date 15:33 2020/1/4
     * @Description 比较返回-1/0/1
     * 如果两个数一样则返回0,如果第一个数比第二个数大则返回1,反之返回-1
     */
    public static int compareTo(double v1, double v2) {
     
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.compareTo(b2);
    }

}


你可能感兴趣的:(BigDecimal计算的工具类)