Java : MathContext with Examples
MathContext (Java SE 17 & JDK 17) API Examples.
You will find code examples on most MathContext methods.
Summary
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
// precision=34 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL128);
static final MathContext DECIMAL32
// precision=7 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL32);
static final MathContext DECIMAL64
// precision=16 roundingMode=HALF_EVEN
System.out.println(MathContext.DECIMAL64);
static final MathContext UNLIMITED
// 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)
final var mc = new MathContext(16);
System.out.println(mc); // precision=16 roundingMode=HALF_UP
MathContext (int setPrecision, RoundingMode setRoundingMode)
final var mc = new MathContext(8, RoundingMode.CEILING);
System.out.println(mc); // precision=8 roundingMode=CEILING
MathContext (String val)
final var mc = new MathContext("precision=32 roundingMode=FLOOR");
System.out.println(mc); // precision=32 roundingMode=FLOOR
Methods
boolean equals (Object x)
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 ()
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 ()
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 ()
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 ()
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