Java's BigDecimal Detailed

A, BigDecimal Overview

BigDecimal Java API class java.math provided in the package, the number of significant bits used for more than 16 accurate calculation. Double precision floating-point variables can handle 16-bit significand, but in practical applications may require larger or smaller number of computing and processing. In general, for those who do not need to accurately calculate the digital precision, we can directly use the Float and Double handling, but Double.valueOf (String) and Float.valueOf (String) lose precision. Therefore, the development, the results if we need accurate calculations, you must use the BigDecimal class operation.

BigDecimal created object, so we can not use the traditional +, -, *, /, etc. Arithmetic operators perform mathematical operations on its direct object, and must call its corresponding method. Method parameters must also be BigDecimal object. Class constructor is a special method, designed to create objects, particularly objects with parameters.

Two, BigDecimal common constructors

2.1, common constructors

  1. BigDecimal(int)

    Create an integer value specified by the parameter object has

  2. BigDecimal(double)

    Create a double value specified by the parameter object has

  3. BigDecimal(long)

    Create a long integer value specified by the parameter object has

  4. BigDecimal(String)

    Creating a string representation of the parameter value to the specified target having

2.2, the use of analysis

Example of use:

        BigDecimal a =new BigDecimal(0.1);
        System.out.println("a values is:"+a);
        System.out.println("=====================");
        BigDecimal b =new BigDecimal("0.1");
        System.out.println("b values is:"+b);
复制代码

Example result:

a values is:0.1000000000000000055511151231257827021181583404541015625
=====================
b values is:0.1
复制代码

Cause Analysis:

1) a certain type of parameter unpredictability of results of double construction method. One might think that BigDecimal newBigDecimal written in Java (0.1) created exactly equal to 0.1 (unscaled value is 1, and whose scale is 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 can not be accurately expressed as a double (or, for that matter, can not be expressed as a binary fraction of any finite length). In this way, passed to the constructor is not exactly equal to the value of 0.1 (although this value is equal to the upper surface).

2) String constructor is entirely predictable: Write newBigDecimal ( "0.1") creates a BigDecimal, which is exactly equal to the expected 0.1. Therefore, in contrast, it is generally recommended to use String constructor priority.

3) When double BigDecimal be used as a source, please note that this configuration provides an accurate conversion method; it does not provide the same results with the following operations: using the Double.toString first (double) method, and then use the BigDecimal (String) construction method, a double into a String. To obtain the results, use static valueOf (double) method.

Three, BigDecimal common method Detailed

3.1, a common method

  1. add(BigDecimal)

    BigDecimal object value is added, the object returns BigDecimal

  2. subtract(BigDecimal)

    BigDecimal object value subtraction, the object returns BigDecimal

  3. multiply(BigDecimal)

    BigDecimal object value is multiplied, the object returns BigDecimal

  4. divide(BigDecimal)

    BigDecimal object divided by the value, the object returns BigDecimal

  5. toString()

    BigDecimal to convert the value into a string object

  6. doubleValue()

    BigDecimal object value converted to a double precision number

  7. floatValue ()

    The converted value BigDecimal object to a single precision

  8. long value ()

    The value of the BigDecimal object converted to long integer

  9. intValue()

    BigDecimal to convert the value to an integer object

3.2, BigDecimal size comparison

compare the size of the java BigDecimal is generally used in the compareTo method bigdemical

int a = bigdemical.compareTo(bigdemical2)
复制代码

Return results of the analysis:

a = -1,表示bigdemical小于bigdemical2;
a = 0,表示bigdemical等于bigdemical2;
a = 1,表示bigdemical大于bigdemical2;
复制代码

Example: a greater than equal to b

new bigdemica(a).compareTo(new bigdemical(b)) >= 0
复制代码

Four, BigDecimal format

Since NumberFormat class format () method can be used BigDecimal object as its argument, it may be utilized BigDecimal significant digits that exceeds 16 percentile monetary value, and generally controls the numerical format.

BigDecimal to use an example to monetary and percentage formatting. First, create a BigDecimal object to a BigDecimal arithmetic operations, respectively, and establish a reference currency percentage formatting Finally BigDecimal object as format () method parameters, and outputs the formatted currency values ​​and percentages.


    NumberFormat currency = NumberFormat.getCurrencyInstance(); //建立货币格式化引用 
    NumberFormat percent = NumberFormat.getPercentInstance();  //建立百分比格式化引用 
    percent.setMaximumFractionDigits(3); //百分比小数点最多3位 
    
    BigDecimal loanAmount = new BigDecimal("15000.48"); //贷款金额
    BigDecimal interestRate = new BigDecimal("0.008"); //利率   
    BigDecimal interest = loanAmount.multiply(interestRate); //相乘
 
    System.out.println("贷款金额:\t" + currency.format(loanAmount)); 
    System.out.println("利率:\t" + percent.format(interestRate)); 
    System.out.println("利息:\t" + currency.format(interest)); 
复制代码

result:

贷款金额: ¥15,000.48 利率: 0.8% 利息: ¥120.00
复制代码

BigDecimal reserved 2 decimal format, then make up less than 0:

public class NumberFormat {
	
	public static void main(String[] s){
		System.out.println(formatToNumber(new BigDecimal("3.435")));
		System.out.println(formatToNumber(new BigDecimal(0)));
		System.out.println(formatToNumber(new BigDecimal("0.00")));
		System.out.println(formatToNumber(new BigDecimal("0.001")));
		System.out.println(formatToNumber(new BigDecimal("0.006")));
		System.out.println(formatToNumber(new BigDecimal("0.206")));
    }
	/**
	 * @desc 1.0~1之间的BigDecimal小数,格式化后失去前面的0,则前面直接加上0。
	 * 2.传入的参数等于0,则直接返回字符串"0.00"
	 * 3.大于1的小数,直接格式化返回字符串
	 * @param obj传入的小数
	 * @return
	 */
	public static String formatToNumber(BigDecimal obj) {
		DecimalFormat df = new DecimalFormat("#.00");
		if(obj.compareTo(BigDecimal.ZERO)==0) {
			return "0.00";
		}else if(obj.compareTo(BigDecimal.ZERO)>0&&obj.compareTo(new BigDecimal(1))<0){
			return "0"+df.format(obj).toString();
		}else {
			return df.format(obj).toString();
		}
	}
}
复制代码

The results are:

3.44
0.00
0.00
0.00
0.01
0.21
复制代码

Five, BigDecimal common exceptions

5.1, occur when abnormal division

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result
复制代码

Cause Analysis:

When the division is not divisible by BigDecimal divide method, there infinite decimal, it will Throws: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

Solution:

decimal divide method provided accurate, such as: divide (xxxxx, 2)

Six, BigDecimal summary

6.1 summary

  1. Reuse requires accurate calculation of the fractional BigDecimal, BigDecimal worse than the performance of double and float, especially when dealing with large, complex operations. Therefore, calculation accuracy is generally not necessary to use BigDecimal.
  2. Try to use parameters of type String constructor.
  3. BigDecimal is immutable (the immutable), the calculation is performed every four time, it will produce a new object, so do the arithmetic operation value to remember to save operation.

6.2, recommended tools

package com.vivo.ars.util;
import java.math.BigDecimal;

/**
 * 用于高精确处理常用的数学运算
 */
public class ArithmeticUtils {
    //默认除法运算精度
    private static final int DEF_DIV_SCALE = 10;

    /**
     * 提供精确的加法运算
     *
     * @param v1 被加数
     * @param v2 加数
     * @return 两个参数的和
     */

    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();
    }

    /**
     * 提供精确的加法运算
     *
     * @param v1 被加数
     * @param v2 加数
     * @return 两个参数的和
     */
    public static BigDecimal add(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.add(b2);
    }

    /**
     * 提供精确的加法运算
     *
     * @param v1    被加数
     * @param v2    加数
     * @param scale 保留scale 位小数
     * @return 两个参数的和
     */
    public static String add(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.add(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 提供精确的减法运算
     *
     * @param v1 被减数
     * @param v2 减数
     * @return 两个参数的差
     */
    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();
    }

    /**
     * 提供精确的减法运算。
     *
     * @param v1 被减数
     * @param v2 减数
     * @return 两个参数的差
     */
    public static BigDecimal sub(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.subtract(b2);
    }

    /**
     * 提供精确的减法运算
     *
     * @param v1    被减数
     * @param v2    减数
     * @param scale 保留scale 位小数
     * @return 两个参数的差
     */
    public static String sub(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.subtract(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 提供精确的乘法运算
     *
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    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();
    }

    /**
     * 提供精确的乘法运算
     *
     * @param v1 被乘数
     * @param v2 乘数
     * @return 两个参数的积
     */
    public static BigDecimal mul(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2);
    }

    /**
     * 提供精确的乘法运算
     *
     * @param v1    被乘数
     * @param v2    乘数
     * @param scale 保留scale 位小数
     * @return 两个参数的积
     */
    public static double mul(double v1, double v2, int scale) {
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return round(b1.multiply(b2).doubleValue(), scale);
    }

    /**
     * 提供精确的乘法运算
     *
     * @param v1    被乘数
     * @param v2    乘数
     * @param scale 保留scale 位小数
     * @return 两个参数的积
     */
    public static String mul(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.multiply(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到
     * 小数点以后10位,以后的数字四舍五入
     *
     * @param v1 被除数
     * @param v2 除数
     * @return 两个参数的商
     */

    public static double div(double v1, double v2) {
        return div(v1, v2, DEF_DIV_SCALE);
    }

    /**
     * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
     * 定精度,以后的数字四舍五入
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示表示需要精确到小数点以后几位。
     * @return 两个参数的商
     */
    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();
    }

    /**
     * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指
     * 定精度,以后的数字四舍五入
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 表示需要精确到小数点以后几位
     * @return 两个参数的商
     */
    public static String div(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v1);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 提供精确的小数位四舍五入处理
     *
     * @param v     需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    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));
        return b.setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 提供精确的小数位四舍五入处理
     *
     * @param v     需要四舍五入的数字
     * @param scale 小数点后保留几位
     * @return 四舍五入后的结果
     */
    public static String round(String v, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b = new BigDecimal(v);
        return b.setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 取余数
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 小数点后保留几位
     * @return 余数
     */
    public static String remainder(String v1, String v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        return b1.remainder(b2).setScale(scale, BigDecimal.ROUND_HALF_UP).toString();
    }

    /**
     * 取余数  BigDecimal
     *
     * @param v1    被除数
     * @param v2    除数
     * @param scale 小数点后保留几位
     * @return 余数
     */
    public static BigDecimal remainder(BigDecimal v1, BigDecimal v2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException(
                    "The scale must be a positive integer or zero");
        }
        return v1.remainder(v2).setScale(scale, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 比较大小
     *
     * @param v1 被比较数
     * @param v2 比较数
     * @return 如果v1 大于v2 则 返回true 否则false
     */
    public static boolean compare(String v1, String v2) {
        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);
        int bj = b1.compareTo(b2);
        boolean res;
        if (bj > 0)
            res = true;
        else
            res = false;
        return res;
    }
}
复制代码

Guess you like

Origin juejin.im/post/5d81e96b51882507ba2269d8