Warning: Use BigDecimal online with caution!

Source: cnblogs.com/zhangyinhua/p/11545305.html

1. Overview of BigDecimal

The API class BigDecimal provided by Java in the java.math package is used to perform precise operations on numbers with more than 16 effective digits. The double-precision floating-point variable double can handle 16 significant digits, but in practical applications, it may be necessary to perform operations and processing on larger or smaller numbers.

In general, we can directly use Float and Double for those numbers that do not need accurate calculation precision, but Double.valueOf(String) and Float.valueOf(String) will lose precision. So in development, if we need accurate calculation results, we must use the BigDecimal class to operate.

What BigDecimal creates is an object, so we cannot use traditional arithmetic operators such as +, -, *, / to directly perform mathematical operations on its object, but must call its corresponding method. The parameters in the method must also be BigDecimal objects. A constructor is a special method of a class designed to create objects, especially objects with parameters.

Two, BigDecimal commonly used constructor

2.1. Commonly used constructors

  • BigDecimal(int)

Creates an object with the integer value specified by the parameter

  • BigDecimal(double)

Creates an object with the double value specified by the argument

  • BigDecimal(long)

Creates an object with the long integer value specified by the parameter

  • BigDecimal(String)

Creates an object with the numeric value specified by the parameter as a string

2.2, use problem analysis

Example usage:

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) The result of the construction method whose parameter type is double is somewhat unpredictable. One might think that writing newBigDecimal(0.1) in Java creates a BigDecimal equal to exactly 0.1 (the unscaled value of 1, which has a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as any finite-length binary fraction). Thus, the value passed into the constructor will not be exactly equal to 0.1 (although apparently equal to that value).

2) The String constructor is completely predictable: writing newBigDecimal("0.1") will create a BigDecimal that is exactly equal to the expected 0.1. Therefore, in comparison, it is generally recommended to use the String construction method first.

3) When double must be used as the source of BigDecimal, note that this constructor provides an exact conversion; it does not provide the same result as using the Double.toString(double) method first, then using BigDecimal(String) Constructor, convert double to String. To get that result, use the static valueOf(double) method.

3. Detailed Explanation of Common Methods of BigDecimal

3.1. Common methods

  • add(BigDecimal)

Adds the values ​​in the BigDecimal object and returns the BigDecimal object

  • subtract(BigDecimal)

Subtract the values ​​in the BigDecimal object and return the BigDecimal object

  • multiply(BigDecimal)

Multiply the values ​​in the BigDecimal object and return the BigDecimal object

  • divide(BigDecimal)

Divides the values ​​in the BigDecimal object and returns the BigDecimal object

  • toString()

Converts a value in a BigDecimal object to a string

  • doubleValue()

Converts the value in the BigDecimal object to a double precision number

  • floatValue()

Converts the value in the BigDecimal object to a single precision number

  • longValue()

Converts a value in a BigDecimal object to a long integer

  • intValue()

Converts a value in a BigDecimal object to an integer

3.2, BigDecimal size comparison

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

int a = bigdemical.compareTo(bigdemical2)

Return result analysis:

a = -1,表示bigdemical小于bigdemical2;
a = 0,表示bigdemical等于bigdemical2;
a = 1,表示bigdemical大于bigdemical2;

Example: a is greater than or equal to b

new bigdemica(a).compareTo(new bigdemical(b)) >= 0

Four, BigDecimal formatting

Since the format() method of the NumberFormat class can use BigDecimal objects as its parameters, BigDecimal can be used to control the formatting of monetary values, percentage values, and general numerical values ​​beyond 16 significant figures.

Take the formatting of currency and percentages using BigDecimal as an example. First, create a BigDecimal object, perform BigDecimal arithmetic operations, respectively establish references to currency and percentage formatting, and finally use the BigDecimal object as a parameter of the format() method to output its formatted currency value and percentage.

Recommend an open source and free Spring Boot practical project:

https://github.com/javastacks/spring-boot-best-practice

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 formatting retains 2 as a decimal, and fills in 0 if it is insufficient:

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 result is:

3.44
0.00
0.00
0.00
0.01
0.21

5. Common exceptions of BigDecimal

5.1. An exception occurs during division

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result

Cause Analysis:

When dividing by the divide method of BigDecimal, when there is no integer division and an infinite cycle of decimals occurs, an exception will be thrown: java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.

Solution:

The divide method sets a precise decimal point, such as: divide(xxxxx,2)

6. Summary of BigDecimal

6.1. Summary

Use BigDecimal when accurate decimal calculation is required. The performance of BigDecimal is worse than that of double and float, especially when dealing with large and complex operations. Therefore, it is not necessary to use BigDecimal for general precision calculations. Try to use constructors whose parameter type is String.

BigDecimal is immutable, and a new object will be generated every time the four arithmetic operations are performed, so remember to save the value after the operation when doing addition, subtraction, multiplication and division.

6.2. Tools recommendation

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

Recent hot article recommendation:

1. 1,000+ Java interview questions and answers (2022 latest version)

2. Brilliant! Java coroutines are coming. . .

3. Spring Boot 2.x tutorial, too comprehensive!

4. Don't fill the screen with explosions and explosions, try the decorator mode, this is the elegant way! !

5. The latest release of "Java Development Manual (Songshan Edition)", download quickly!

Feel good, don't forget to like + forward!

Guess you like

Origin blog.csdn.net/youanyyou/article/details/131168372