Create BigDecimals

BigDecimal(BigInteger val)
Converts a BigInteger into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale)
Converts a BigInteger with scale into a BigDecimal.
BigDecimal(BigInteger unscaledVal, int scale, MathContext mc)
Converts a BigInteger and an int scale into a BigDecimal, with rounding according to the context settings.
BigDecimal(BigInteger val, MathContext mc)
Converts a BigInteger into a BigDecimal rounding according to the context settings.
BigDecimal(char[] in)
Converts a character array representation of a BigDecimal into a BigDecimal, accepting the same sequence of characters as the BigDecimal(String) constructor.
BigDecimal(char[] in, int offset, int len)
Converts a character array representation of a BigDecimal into a BigDecimal.
BigDecimal(char[] in, int offset, int len, MathContext mc)
Converts a character array representation of a BigDecimal into a BigDecimal, according to the context settings.
BigDecimal(char[] in, MathContext mc)
Converts a character array representation of a BigDecimal into a BigDecimal, according to the context settings.
BigDecimal(double val)
Converts a double into a BigDecimal.
BigDecimal(double val, MathContext mc)
Converts a double into a BigDecimal, with the context settings.
BigDecimal(int val)
Converts an int into a BigDecimal.
BigDecimal(int val, MathContext mc)
Converts an int into a BigDecimal, with rounding according to the context settings.
BigDecimal(long val)
Converts a long into a BigDecimal.
BigDecimal(long val, MathContext mc)
Converts a long into a BigDecimal, with rounding according to the context settings.
BigDecimal(String val)
Converts the string representation of a BigDecimal into a BigDecimal.
BigDecimal(String val, MathContext mc)
Converts the string into a BigDecimal, with rounding according to the context settings.

import java.math.BigDecimal;
 

public class Main {
 
    public static void main(String[] args) {
        System.out.println(new BigDecimal(1f));
        System.out.println(new BigDecimal(2f));
        
    }
}

The output:


1
2

import java.math.BigDecimal;
import java.math.MathContext;
 

public class Main {
 
    public static void main(String[] args) {
        BigDecimal first = new BigDecimal(1f);
        BigDecimal second = new BigDecimal(1000f);
        BigDecimal result1 = new BigDecimal(first.doubleValue() / second.doubleValue());
        BigDecimal result2 = first.divide(second, MathContext.DECIMAL128);
 
        System.out.println(result1);
        System.out.println(result2);
        System.out.println((first.doubleValue() / second.doubleValue()));
 
    }
}

The output:


0.001000000000000000020816681711721685132943093776702880859375
0.001
0.0010
Home 
  Java Book 
    Essential Classes  

BigDecimal:
  1. BigDecimal class
  2. Constants for One, Ten and Zero
  3. Rounding mode
  4. Create BigDecimals
  5. Methods used to do calculation
  6. Convert BigDecimal to primitive data types
  7. Compare two BigDecimal
  8. Move decimal point
  9. Scale and precision
  10. Convert BigDecimal to String
  11. Remove the trailing zeros
  12. Convert double and long to BigDecimal
  13. Calculating Euler's number e with BigDecimal