Commonly used in business computing BigDecimal detailed explanation

Commonly used in business computing BigDecimal detailed explanation

1. BigDecimal usage introduction

Use java.math.BigDecimal in business calculations
. What BigDecimal creates is an object, we cannot use the traditional +, -, *, / and other arithmetic operators to directly perform mathematical operations on the object, but must call its corresponding method. The parameters in the method must also be BigDecimal objects.

Second, create a BigDecimal object:

BigDecimal bigDecimal = new BigDecimal(xxx); Among them, *xxx* can be * integer data* , or * string data of digital content* , but cannot be floating-point data, such as:

3. Convert BigDecimal to Str

/**
     * BigDecimal转换为字符串类型
     * @param bigDecimal
     * @return
     */
    private String convertStr(BigDecimal bigDecimal) {

        if(null == bigDecimal){
            return null;
        }
        DecimalFormat decimalFormat = new DecimalFormat("#,###.##");
        return decimalFormat.format(bigDecimal);
    }

Four, BigDecimal comparison size

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

 

 

Guess you like

Origin blog.csdn.net/weixin_56602545/article/details/130001646