Java : MathContext with Examples
MathContext (Java SE 17 & JDK 17) API Examples.
You will find code examples on most MathContext methods.
Summary
Immutable objects which encapsulate the context settings which describe certain rules for numerical operators, such as those implemented by the BigDecimal class.
final var three = new BigDecimal(3);
System.out.println(three); // 3
System.out.println(three.scale()); // 0
// 10÷3
System.out.println(BigDecimal.TEN.divide(three, RoundingMode.CEILING)); // 4
{
final var mc = new MathContext(3, RoundingMode.CEILING);
System.out.println(mc); // precision=3 roundingMode=CEILING
System.out.println(BigDecimal.TEN.divide(three, mc)); // 3.34
}
{
final var mc = new MathContext(3, RoundingMode.FLOOR);
System.out.println(mc); // precision=3 roundingMode=FLOOR
System.out.println(BigDecimal.TEN.divide(three, mc)); // 3.33
}
Fields
static final MathContext DECIMAL128
A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal128 format, 34 digits, and a rounding mode of HALF_EVEN.
// precision=34 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL128);
static final MathContext DECIMAL32
A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal32 format, 7 digits, and a rounding mode of HALF_EVEN.
// precision=7 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL32);
static final MathContext DECIMAL64
A MathContext object with a precision setting matching the precision of the IEEE 754-2019 decimal64 format, 16 digits, and a rounding mode of HALF_EVEN.
// precision=16 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL64);
static final MathContext UNLIMITED
A MathContext object whose settings have the values required for unlimited precision arithmetic.
// precision=0 roundingMode=HALF_UP
System.out.println(MathContext.UNLIMITED);
// 10÷3
final var three = new BigDecimal(3);
System.out.println(three); // 3
// ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
//BigDecimal.TEN.divide(three, MathContext.UNLIMITED);
Constructors
MathContext (int setPrecision)
Constructs a new MathContext with the specified precision and the HALF_UP rounding mode.
final var mc = new MathContext(16);
System.out.println(mc); // precision=16 roundingMode=HALF_UP
MathContext (int setPrecision, RoundingMode setRoundingMode)
Constructs a new MathContext with a specified precision and rounding mode.
final var mc = new MathContext(8, RoundingMode.CEILING);
System.out.println(mc); // precision=8 roundingMode=CEILING
MathContext (String val)
Constructs a new MathContext from a string.
final var mc = new MathContext("precision=32 roundingMode=FLOOR");
System.out.println(mc); // precision=32 roundingMode=FLOOR
Methods
boolean equals (Object x)
Compares this MathContext with the specified Object for equality.
final var mc1 = new MathContext(8, RoundingMode.CEILING);
final var mc2 = new MathContext(8, RoundingMode.CEILING);
System.out.println(mc1 != mc2); // true
System.out.println(mc1.equals(mc2)); // true
final var mc3 = new MathContext(8, RoundingMode.FLOOR);
final var mc4 = new MathContext(4, RoundingMode.FLOOR);
System.out.println(mc1.equals(mc3)); // false
System.out.println(mc1.equals(mc4)); // false
System.out.println(mc3.equals(mc4)); // false
int getPrecision ()
Returns the precision setting.
final var mc = new MathContext(32);
System.out.println(mc); // precision=32 roundingMode=HALF_UP
System.out.println(mc.getPrecision()); // 32
System.out.println(MathContext.DECIMAL64); // precision=16 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL64.getPrecision()); // 16
RoundingMode getRoundingMode ()
Returns the roundingMode setting.
final var mc = new MathContext(8, RoundingMode.FLOOR);
System.out.println(mc); // precision=8 roundingMode=FLOOR
System.out.println(mc.getRoundingMode()); // FLOOR
System.out.println(MathContext.DECIMAL64); // precision=16 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL64.getRoundingMode()); // HALF_EVEN
int hashCode ()
Returns the hash code for this MathContext.
final var mc1 = new MathContext(8, RoundingMode.CEILING);
final var mc2 = new MathContext(8, RoundingMode.CEILING);
System.out.println(mc1 != mc2); // true
System.out.println(mc1.hashCode()); // -1237294828
System.out.println(mc2.hashCode()); // -1237294828
final var mc3 = new MathContext(8, RoundingMode.FLOOR);
final var mc4 = new MathContext(4, RoundingMode.FLOOR);
System.out.println(mc3.hashCode()); // -321581207
System.out.println(mc4.hashCode()); // -321581211
String toString ()
Returns the string representation of this MathContext.
final var mc = new MathContext(8, RoundingMode.FLOOR);
final var str1 = mc.toString();
System.out.println(str1); // precision=8 roundingMode=FLOOR
final var str2 = MathContext.DECIMAL64.toString();
System.out.println(str2); // precision=16 roundingMode=HALF_EVEN