BigDecimal compare the size and use of basic addition, subtraction

// size comparison: 
int A = bigdemical.compareTo (bigdemical2)
 // A = -1, represents less than bigdemical bigdemical2;
 // A = 0, indicates bigdemical equal bigdemical2;
 // A =. 1, showing greater than bigdemical bigdemical2; 

Boolean In Flag value1.compareTo = (BigDecimal.ZERO) == 1 && value1.compareTo (BigDecimal.ONE) == -1 ;
 IF (In Flag) { 
    System.out.println ( "VALUE1 is greater than 0 and less than 1" ); 
} 


// add Less multiplication and division: 
the BigDecimal bignum1 = new new the BigDecimal ( "10" );   
the BigDecimal bignum2 = new new the BigDecimal ( ". 5" );   
the BigDecimal bignum3 =null ;   
  
// adder   
bignum3 =   bignum1.add (bignum2);        
System.out.println ( "and are:" + bignum3);   
  
// subtractor   
bignum3 = bignum1.subtract (bignum2);   
System.out.println ( "difference is : "+ bignum3);   
  
// multiplication   
bignum3 = bignum1.multiply (bignum2);   
System.out.println ( " product is: "+ bignum3);   
  
// division   
bignum3 = bignum1.divide (bignum2);   
System.out.println ( "Suppliers are:" + bignum3); 



// BigDecimal object into the base data: 

// value BigDecimal object into corresponding character string
bigDecimalOne.toString (); 

// the BigDecimal double value of the return 
bigDecimalOne.doubleValue (); 

// the BigDecimal to return a float 
bigDecimalOne.floatValue (); 

// the BigDecimal value of the long return 
bigDecimalOne.longValue (); 

// the BigDecimal to return int value 
bigDecimalOne.intValue (); 


// Java rounding mode: 

// the first method: two decimals 
Double DB = bigDecimalTwo.setScale (2 , as RoundingMode.HALF_UP) .doubleValue (); 
System.out.println ( db); 
 
// The second method: two decimal places 
DecimalFormat df = new new DecimalFormat ( "# 00." ); 
df.format (bigDecimalTwo);

 

Guess you like

Origin www.cnblogs.com/yysbolg/p/11095673.html