BigDecimal of common Java class libraries

BigDecimal

concept

When running 0.1 + 0.2, the operation errors of float and double are found. Because float and double types may have errors in operation, Java. Com needs to be used math. BigDecimal class

Common construction methods

public BigDecimal(String val){

}

common method

add​(BigDecimal augend)

  • Function: return BigDecimal with the value of (this + August)
BigDecimal b1 = new BigDecimal("0.1");
BigDecimal b2 = new BigDecimal("0.2");
BigDecimal b3 = b1.add(b2);
System.out.println(b3);
//0.3
//Take out the double in BigDecimal
b3.doubleValue();
//MathContext indicates the decimal length
BigDecimal b3 = b1.add(b2, MathContext.DECIMAL32);

subtract​(BigDecimal subtrahend)

  • Function: return BigDecimal with the value of (this - subtrahend) and the proportion of max (this. Scale(), subtrahend scale()) .

divide​(BigDecimal divisor)

  • Function: return BigDecimal with the value of (this / Division)
BigDecimal b1 = new BigDecimal("1.5");
BigDecimal b2 = new BigDecimal("3");
BigDecimal b3 = b1.divide(b2);
System.out.println(b3);
//0.5
//When calculating 4 / 3, an error is reported because the output is an infinite circular decimal

multiply​(BigDecimal multiplicand)

  • Function: return BigDecimal with the value (this) × multiplicand), whose ratio is (this. Scale() + multiplicand scale()) .
BigDecimal b1 = new BigDecimal("12.34");
BigDecimal b2 = new BigDecimal("11.11");
BigDecimal b3 = b1.multiply(b2, MathContext.DECIMAL32);
System.out.println(b3);
//137.0974

divide​(BigDecimal divisor, int scale, RoundingMode roundingMode)

  • Function: return BigDecimal, whose value is (this / Division), and the controller precision scale (several digits after the decimal point) to control its s rounding mode
BigDecimal b1 = new BigDecimal("4");
BigDecimal b2 = new BigDecimal("3");
BigDecimal b3 = b1.divide(b2, 3, RoundingMode.DOWN);
System.out.println(b3);
//1.33
  • Other division operations
    • Divideetointegralvalue (BigDecimal division, mathcontext MC), returns the integer solution of the quotient
    • Divideandremander (BigDecimal divisor, MathContext mc), two BigDecimal operands that return quotient and remainder

remainder​(BigDecimal divisor)

  • Function: calculate the remainder. Note that it is different from modular operation. When the signs of x and y are the same, the results of the two functions are the same; When the symbols of x and y are different, the symbol of the result of rem function is the same as that of x, while mod is the same as that of Y.

  • This is because the generation mechanism of these two functions is different. rem function adopts fix function (round to 0) and mod function adopts floor function (round to infinity)

    • rem(x, y) returns x-n*y. if y is not equal to 0, n = fix(x./y)
    • mod(x, y) returns x-n*y. if y is not equal to 0, n = floor(x./y)

Reference source: https://www.runoob.com/w3cnote/remainder-and-the-modulo.html

BigDecimal b1 = new BigDecimal("-5.2");
BigDecimal b2 = new BigDecimal("2");
BigDecimal b3 = b1.remainder(b2);
System.out.println(b3);
//fix(-5.2/2) = -2; 5.2-(-2)*2 = -1.2

pow​(int n, MathContext mc)

  • Function: return BigDecimal with the value (this^n)
BigDecimal b1 = new BigDecimal("12.34");
BigDecimal b2 = new BigDecimal("11.11");
BigDecimal b3 = b1.pow(8, MathContext.DECIMAL32);
System.out.println(b3);
//5.376768E+8

negate​()

  • Function: return BigDecimal, whose value is (- this), and round it according to the context setting (MathContext).
  • plus(), return + this

round​(MathContext mc)

  • Function: rounding, MathContext setting reserved digits

public int compareTo​(BigDecimal val)

  • Function: compare the size of two BigDecimal
BigDecimal b1 = new BigDecimal("3");
BigDecimal b2 = new BigDecimal("3");
int b3 = b1.compareTo(b2);
System.out.println(b3);
//3,3 -> 0
//2,3 -> -1
//4,3 -> 1

public boolean equals​(Object x)

  • Function: compare this BigDecimal with the specified Object. Unlike compareTo, this method only considers that the values and scales of two BigDecimal objects are equal (so 2.0 is not equal to 2.00 when compared with this method).
BigDecimal b1 = new BigDecimal("3");
BigDecimal b2 = new BigDecimal("3.00");
System.out.println(b1.equals(b2));
//false

public BigDecimal max​(BigDecimal val)

  • Function: compare and return the maximum value
BigDecimal b1 = new BigDecimal("3.5");
BigDecimal b2 = new BigDecimal("3");
BigDecimal b3 = b1.max(b2);
System.out.println(b3);
//3.5
//BigDecimal b3 = b1.min(b2);

public static BigDecimal valueOf​(double val)

  • Function: convert a double into BigDecimal, and use double to represent double through the provided standard string ToString (double) method
double val = 1.2222;
BigDecimal b1 = BigDecimal.valueOf(val);
System.out.println(b1);

public double doubleValue()

  • Function: convert BigDecimal to double.

public BigDecimal movePointLeft​(int n)

  • Function: move n decimal places to the left
BigDecimal b1 = new BigDecimal("3.5");
BigDecimal b3 = b1.movePointLeft(8);
System.out.println(b3);
//3.5E-8
//BigDecimal b3 = b1.movePointLeft(8);

precision()

  • Function: seeking precision
BigDecimal b1 = new BigDecimal("12.346");
System.out.println(b1.precision());

int scale()

  • Function: returns the number of digits after the decimal point
BigDecimal b1 = new BigDecimal("-5.22323");
System.out.println(b1.scale());
//5

scaleByPowerOfTen​(int n)

  • Function: multiply by 10^n power
BigDecimal b1 = new BigDecimal("-5.22323");
BigDecimal b3 = b1.scaleByPowerOfTen(2);
System.out.println(b3);
//-522.323

setScale​(int newScale, RoundingMode roundingMode)

  • Function: set the number of digits after the decimal point
BigDecimal b1 = new BigDecimal("-5.22323");
BigDecimal b3 = b1.setScale(2, RoundingMode.DOWN);
System.out.println(b3);
//-5.22

stripTrailingZeros()

  • Function: subtract trailing extra 0
BigDecimal b1 = new BigDecimal("-522000");
BigDecimal b2 = new BigDecimal("2.00");
BigDecimal b3 = b1.stripTrailingZeros();
BigDecimal b4 = b2.stripTrailingZeros();
System.out.println(b3);
//-5.22E5
System.out.println(b4);
//2

public BigDecimal ulp()

  • Function: returns the BigDecimal of the ulp (last unit) of this BigDecimal. The ulp of a non-zero BigDecimal value is the positive distance between the value and the BigDecimal value, which then has the same number of digits in quantity. The ulp of zero is numerically equal to 1 and the ratio is this. The results are stored in the same proportion as this, so the results of zero and non-zero values are equal to [1, this.scale()].
BigDecimal b1 = new BigDecimal("-12.324");
BigDecimal b2 = new BigDecimal("212120");
BigDecimal b3 = b1.ulp();
BigDecimal b4 = b2.ulp();
System.out.println(b3);
//0.001
System.out.println(b4);
//1
//"0" -> 1
//All integers are 1

MathContext field

MathContext fieldSignificant number
DECIMAL327 digits
DECIMAL6416 bit
DECIMAL12834 bits
UNLIMITEDinfinite

RoundingMode

UP DOWN CEILING FLOOR HALF_UP HALF_DOWN HALF_EVEN UNNECESSARY

Keywords: Java

Added by hi-liter on Sun, 30 Jan 2022 09:01:45 +0200