Precise integer calculation in Java - Bigdecimal learning summary and tool class

  Essay: With the recent work needs, looking back needs to involve some accurate data calculations, you need to use Bigdecimal, simply take advantage of the spare time to organize and collect about the use of Bigdecimal, due to time reasons, the arrangement is not special Detailed, but I believe that the general use should also be sorted out, if there are any omissions, please suggest it, thank you for your support.

1. Introduction

The API class BigDecimal provided by Java in the java.math package is used to perform precise operations on numbers with more than 16 significant digits. The double-precision floating-point variable double can handle a 16-bit significand. In practical applications, operations and processing of larger or smaller numbers are required. float and double can only be used for scientific calculations or engineering calculations, and java.math.BigDecimal is used in commercial calculations. What BigDecimal creates is an object. We cannot use traditional +, -, *, / and other arithmetic operators to directly perform mathematical operations on its object, but must call its corresponding method. The parameters in the method must also be BigDecimal objects. Constructors are special methods of classes designed to create objects, especially objects with parameters.

2. Description of the constructor

BigDecimal(int) creates an object with the integer value specified by the argument.

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 argument.

BigDecimal(String) Creates an object with the numeric value specified by the argument as a string.

3. Method description

add(BigDecimal) Adds the values ​​in the BigDecimal object and returns this object.

subtract(BigDecimal) Subtracts the values ​​in the BigDecimal object and returns this object.

multiply(BigDecimal) Multiplies the values ​​in the BigDecimal object and returns this object.

divide(BigDecimal) Divide the values ​​in the BigDecimal object and return this object.

toString() converts the value of a BigDecimal object to a string.

doubleValue() returns the value in a BigDecimal object as a double.

floatValue() returns the value in a BigDecimal object as a single-precision number.

longValue() returns the value in a BigDecimal object as a long integer.

intValue() returns the value in the BigDecimal object as an integer.

4. Common methods

Two decimal places, or four decimal places

Tool class code: This tool class can retain several decimal places for custom multiplication and division

package com.ph.util;

import java.math.BigDecimal;
/**
* Since Java's simple types cannot perform precise operations on floating-point numbers, this utility class provides precise
floating-point operations, including addition, subtraction, multiplication, and division, and rounding.
*/
public class BigDecimalUtil{

//Default division precision
private static final int DEF_DIV_SCALE = 10; //This class cannot be instantiated

private BigDecimalUtil(){

}
/**
* Provides exact addition operation.
* @param v1 addend
* @param v2 addend
* @return sum of two parameters
* return double type data
*/
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();
}

/**
* Provides an exact addition operation.
* @param v1 addend
* @param v2 addend
* @return sum of two parameters
* return string type data
*/
public static String addToString(double v1, double v2){
BigDecimal b1 = new BigDecimal(Double.toString (v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).toString();
}

/**
* Provides exact subtraction.
* @param v1 Minuend
* @param v2 Minuend
* @return Difference of two parameters
* Returns double type data
*/
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();
}

/**
* Provides exact subtraction.
* @param v1 Minuend
* @param v2 Minuend
* @return Difference of two parameters
* Returns string type data
*/
public static String subString(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString (v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).toString();
}

/**
* Provides exact multiplication.
* @param v1 multiplicand
* @param v2 multiplier
* @return the product of two parameters
* return double type data
*/
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();
}

/**
* Provides exact multiplication.
* @param v1 Multiplicand
* @param v2 Multiplier
* @param scale indicates that several digits after the decimal point need to be preserved
* @return the product of two parameters
* returns double type data
*/
public static double mulByScale(double v1,double v2 ,int scale){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).setScale(scale,BigDecimal.ROUND_HALF_UP). doubleValue();
}

/**
* Provides exact multiplication.
* @param v1 Multiplicand
* @param v2 Multiplier
* @param scale indicates that several decimal places need to be retained
* @return The product of the two parameters
* Returns string type data
*/
public static String mulByScaleToString(double v1, double v2, int scale){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2)) ;
return b1.multiply(b2).setScale(scale,BigDecimal.ROUND_HALF_UP).toString();
}

/**
* Provides a (relatively) accurate division operation, which is accurate to
* after the decimal point in the case of incomplete division 10 digits, subsequent numbers are rounded up.
* @param v1 dividend
* @param v2 divisor
* @return quotient of two parameters
*/
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}

/**
* provide (relative ) exact division operation. When there is an inexhaustible division, the scale parameter specifies
the precision *, and the following numbers are rounded up.
* @param v1 dividend
* @param v2 divisor
* @param scale indicates that it needs to be accurate to a few decimal places.
* @return the quotient of two parameters
*/
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 b2.divide(b1,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}

/**
* Provides accurate decimal rounding.
* @param v the number to be rounded
* @param scale the number of decimal places to keep
* @return the rounded result
*/
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();
}
};

  

  Well, I will share about Bigdecimal here. If you think it is good, please give it a like and add a follow (*^▽^*)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326400936&siteId=291194637
Recommended