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
Java Book
Essential Classes
BigDecimal:
- BigDecimal class
- Constants for One, Ten and Zero
- Rounding mode
- Create BigDecimals
- Methods used to do calculation
- Convert BigDecimal to primitive data types
- Compare two BigDecimal
- Move decimal point
- Scale and precision
- Convert BigDecimal to String
- Remove the trailing zeros
- Convert double and long to BigDecimal
- Calculating Euler's number e with BigDecimal