Basic usage of BigDecimal

1. Addition, subtraction, multiplication and division

BigDecimal bignum1 = new BigDecimal("10");  
BigDecimal bignum2 = new BigDecimal("5");  
BigDecimal bignum3 = null;  
  
//加法  
bignum3 =  bignum1.add(bignum2);       
System.out.println("和 是:" + bignum3);  
  
//减法  
bignum3 = bignum1.subtract(bignum2);  
System.out.println("差  是:" + bignum3);  
  
// Multiplication   
bignum3 = bignum1.multiply (bignum2);   
System. Out .println ( "The product is: " + bignum3);   
  
// Division   
bignum3 = bignum1.divide (bignum2);   
System. Out .println ( "The quotient is: " + bignum3); 

// Division (rounded) 
bignum3 = bignum1.divide (bignum2, 2 , BigDecimal.ROUND_HALF_UP);  

 

Explain the divide

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

scale refers to the number of digits after your decimal point. For example, 123.456 has a scale of 3.

RoundingMode is as follows:
1. ROUND_UP
rounding mode rounded away from zero.
Always increase the number before discarding the non-zero part (always add 1 to the number before the non-zero discard part)
Note that this rounding mode will never reduce the size of the calculated value.

2.
Rounding mode with ROUND_DOWN close to zero.
Do not increase the number before discarding a part (never add 1 to the number before the discarded part, that is, truncate).
Note that this rounding mode will never increase the size of the calculated value.

3. ROUND_CEILING
is a rounding mode close to positive infinity.
If BigDecimal is positive, the rounding behavior is the same as ROUND_UP;
if negative, the rounding behavior is the same as ROUND_DOWN.
Note that this rounding mode never reduces the calculated value.

4. ROUND_FLOOR is
close to the rounding mode of negative infinity.
If BigDecimal is positive, the rounding behavior is the same as ROUND_DOWN;
if negative, the rounding behavior is the same as ROUND_UP.
Note that this rounding mode will never increase the calculated value.

5. ROUND_HALF_UP rounds
to the "closest" digit. If the distance to two adjacent digits is equal, it is rounded up.
If the discarded part> = 0.5, the rounding behavior is the same as ROUND_UP; otherwise, the rounding behavior is the same as ROUND_DOWN.
Note that this is the rounding mode (rounding) that most of us learned in elementary school.

6. ROUND_HALF_DOWN rounds
to the "closest" digit. If the distance to two adjacent digits is equal, it is rounded up.
If the discarded part is> 0.5, the rounding behavior is the same as ROUND_UP; otherwise, the rounding behavior is the same as ROUND_DOWN (five rounds).

7. ROUND_HALF_EVEN (banker's rounding method) rounds
to the "closest" number, and if the distance to two adjacent numbers is equal, rounds to the adjacent even number.
If the number to the left of the discarded part is odd, the rounding behavior is the same as ROUND_HALF_UP;
if it is even, the rounding behavior is the same as ROUND_HALF_DOWN.
Note that when repeating a series of calculations, this rounding mode can minimize accumulation errors.
This rounding mode is also called "banker's rounding method" and is mainly used in the United States. Rounding off, five points in two cases.
If the previous bit is an odd number, the bit is entered, otherwise it is discarded.
The following example retains one decimal place, so the result of this rounding method.
1.15> 1.2 1.25> 1.2

8. ROUND_UNNECESSARY
asserts that the requested operation has accurate results, so no rounding is required.
If this rounding mode is specified for operations that obtain accurate results, an ArithmeticException is thrown.

 

Second, compare the size

if(A.compareTo(B) == -1){
    System.out.println("A小于B");
}
 
if(A.compareTo(B) == 0){
    System.out.println("A等于B");
}
 
if(A.compareTo(B) == 1){
    System.out.println("A大于B");
}
 
if(A.compareTo(B) > -1){
    System.out.println("A大于等于B");
}
 
if (A.compareTo (B) < 1 ) { 
    System. out .println ( " A is less than or equal to B " ); 
}

 

Third, keep the decimal

// The first method: keep two decimal places 
double db = bigDecimalTwo.setScale ( 2 , RoundingMode.HALF_UP) .doubleValue (); 
System. Out .println (db); 
 
// The second method: keep two decimal places 
DecimalFormat df = new DecimalFormat ( " # .00 " ); 
df.format (bigDecimalTwo);

(to be continued)

Guess you like

Origin www.cnblogs.com/hunttown/p/12714951.html