Java Tutorial/Data Type/BigDecimal

Материал из Java эксперт
Перейти к: навигация, поиск

Convert Object to BigDecimal

import java.math.BigDecimal;
import java.math.BigInteger;
/**
 * Utility methods for math classes
 * 
 * @author etirelli
 */
public class MathUtils {
    
    public static BigDecimal getBigDecimal( Object value ) {
        BigDecimal ret = null;
        if( value != null ) {
            if( value instanceof BigDecimal ) {
                ret = (BigDecimal) value;
            } else if( value instanceof String ) {
                ret = new BigDecimal( (String) value );
            } else if( value instanceof BigInteger ) {
                ret = new BigDecimal( (BigInteger) value );
            } else if( value instanceof Number ) {
                ret = new BigDecimal( ((Number)value).doubleValue() );
            } else {
                throw new ClassCastException("Not possible to coerce ["+value+"] from class "+value.getClass()+" into a BigDecimal.");
            }
        }
        return ret;
    }

}





Create a BigDecimal vis string

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.add(bd2);
  }
}





Create Big Decimal Values via a long

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.add(bd2);
  }
}





Create Big Decimal Values via a string

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.add(bd2);
  }
}





Divide one BigDecimal from another BigDecimal

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
  }
}





Do math operation for BigDecimal

import java.math.BigDecimal;
public class Main {
  public static void main(String[] args) {
    BigDecimal decimalA = new BigDecimal("123456789012345");
    BigDecimal decimalB = new BigDecimal("10");
    decimalA = decimalA.add(decimalB);
    System.out.println("decimalA = " + decimalA);
    decimalA = decimalA.multiply(decimalB);
    System.out.println("decimalA = " + decimalA);
    decimalA = decimalA.subtract(decimalB);
    System.out.println("decimalA = " + decimalA);
    decimalA = decimalA.divide(decimalB);
    System.out.println("decimalA = " + decimalA);
    decimalA = decimalA.pow(2);
    System.out.println("decimalA = " + decimalA);
    decimalA = decimalA.negate();
    System.out.println("decimalA = " + decimalA);
  }
}





Multiply one BigDecimal to another BigDecimal

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.multiply(bd2);
  }
}





Negate a BigDecimal

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    bd1 = bd1.negate();
  }
}





Operate with big decimal values

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create via a string
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.add(bd2);
    bd1 = bd1.multiply(bd2);
    bd1 = bd1.subtract(bd2);
    bd1 = bd1.divide(bd2, BigDecimal.ROUND_UP);
    bd1 = bd1.negate();
  }
}





Round a double

import java.math.BigDecimal;
public class Main {
  public static void main(String args[]) {
    BigDecimal bd = new BigDecimal(3.14159);
    bd = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
    System.out.println(bd);
  }
}
//3.14





Setting the Decimal Place of a Big Decimal Value

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    int decimalPlaces = 2;
    BigDecimal bd = new BigDecimal("123456789.0123456890");
     
    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
    String string = bd.toString();
  }
}





Subtract from one BigDecimal another BigDecimal

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    BigDecimal bd1 = new BigDecimal("123456789.0123456890");
    // Create via a long
    BigDecimal bd2 = BigDecimal.valueOf(123L);
    bd1 = bd1.subtract(bd2);
  }
}





Truncates the big decimal value

import java.math.BigDecimal;
public class Main {
  public static void main(String[] argv) throws Exception {
    int decimalPlaces = 2;
    BigDecimal bd = new BigDecimal("123456789.0123456890");
     
    bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_DOWN);
    String string = bd.toString();
  }
}