You'll have to create your own wrapper class that has your default MathContext like this full example:
In this example, I used the library (https://projectlombok.org)
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BigDecimalFactory {
//Seguindo a precisão máxima do PowerBuilder e Oracle Database
public static final MathContext DEFAULT_CONTEXT = new MathContext(120 , RoundingMode.HALF_UP);
private static final BigDecimalFactory FACTORY = new BigDecimalFactory();
private class SBigDecimal extends BigDecimal {
public SBigDecimal(BigDecimal bdNumber) {
super(bdNumber.toPlainString());
}
public SBigDecimal(String stringNumber) {
super(stringNumber);
}
@Override
public BigDecimal divide(BigDecimal divisor) {
return new SBigDecimal(super.divide(divisor, DEFAULT_CONTEXT).stripTrailingZeros());
}
@Override
public BigDecimal divide(BigDecimal divisor, MathContext mc) {
return new SBigDecimal(super.divide(divisor, mc));
}
@Override
public BigDecimal divideToIntegralValue(BigDecimal divisor) {
return new SBigDecimal(super.divideToIntegralValue(divisor));
}
@Override
public BigDecimal divideToIntegralValue(BigDecimal divisor, MathContext mc) {
return new SBigDecimal(super.divideToIntegralValue(divisor, mc));
}
@Override
public BigDecimal remainder(BigDecimal divisor) {
return new SBigDecimal(super.remainder(divisor));
}
@Override
public BigDecimal remainder(BigDecimal divisor, MathContext mc) {
return new SBigDecimal(super.remainder(divisor, mc));
}
@Override
public BigDecimal pow(int n) {
return new SBigDecimal(super.pow(n));
}
@Override
public BigDecimal pow(int n, MathContext mc) {
return new SBigDecimal(super.pow(n, mc));
}
@Override
public BigDecimal abs() {
return new SBigDecimal(super.abs());
}
@Override
public BigDecimal abs(MathContext mc) {
return new SBigDecimal(super.abs(mc));
}
@Override
public BigDecimal negate() {
return new SBigDecimal(super.negate());
}
@Override
public BigDecimal negate(MathContext mc) {
return new SBigDecimal(super.negate(mc));
}
@Override
public BigDecimal plus() {
return new SBigDecimal(super.plus());
}
@Override
public BigDecimal plus(MathContext mc) {
return new SBigDecimal(super.plus(mc));
}
@Override
public BigDecimal round(MathContext mc) {
return new SBigDecimal(super.round(mc));
}
@Override
public BigDecimal setScale(int newScale, RoundingMode roundingMode) {
return new SBigDecimal(super.setScale(newScale, roundingMode));
}
@Override
public BigDecimal setScale(int newScale, int roundingMode) {
return new SBigDecimal(super.setScale(newScale, roundingMode));
}
@Override
public BigDecimal setScale(int newScale) {
return new SBigDecimal(super.setScale(newScale));
}
@Override
public BigDecimal movePointLeft(int n) {
return new SBigDecimal(super.movePointLeft(n));
}
@Override
public BigDecimal movePointRight(int n) {
return new SBigDecimal(super.movePointRight(n));
}
@Override
public BigDecimal scaleByPowerOfTen(int n) {
return new SBigDecimal(super.scaleByPowerOfTen(n));
}
@Override
public BigDecimal stripTrailingZeros() {
return new SBigDecimal(super.stripTrailingZeros());
}
@Override
public BigDecimal min(BigDecimal val) {
return new SBigDecimal(super.min(val));
}
@Override
public BigDecimal max(BigDecimal val) {
return new SBigDecimal(super.max(val));
}
@Override
public BigDecimal ulp() {
return new SBigDecimal(super.ulp());
}
@Override
public BigDecimal add(BigDecimal augend, MathContext mc) {
return new SBigDecimal(super.add(augend, mc));
}
@Override
public BigDecimal subtract(BigDecimal subtrahend) {
return new SBigDecimal(super.subtract(subtrahend));
}
@Override
public BigDecimal subtract(BigDecimal subtrahend, MathContext mc) {
return new SBigDecimal(super.subtract(subtrahend, mc));
}
@Override
public BigDecimal multiply(BigDecimal multiplicand) {
return new SBigDecimal(super.multiply(multiplicand));
}
@Override
public BigDecimal multiply(BigDecimal multiplicand, MathContext mc) {
return new SBigDecimal(super.multiply(multiplicand, mc));
}
@Override
public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) {
return new SBigDecimal(super.divide(divisor, scale, roundingMode));
}
@Override
public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
return new SBigDecimal(super.divide(divisor, scale, roundingMode));
}
@Override
public BigDecimal divide(BigDecimal divisor, int roundingMode) {
return new SBigDecimal(super.divide(divisor, roundingMode));
}
@Override
public BigDecimal divide(BigDecimal divisor, RoundingMode roundingMode) {
return new SBigDecimal(super.divide(divisor, roundingMode));
}
@Override
public BigDecimal add(BigDecimal augend) {
return new SBigDecimal(super.add(augend));
}
}
public BigDecimal internalCreate(String stringNumber) {
return new SBigDecimal(stringNumber);
}
public static BigDecimal create(BigDecimal b) {
return FACTORY.internalCreate(b.toString());
}
public static BigDecimal create(String stringNumber) {
return FACTORY.internalCreate(stringNumber);
}
public static BigDecimal create(Long longNumber) {
return FACTORY.internalCreate(longNumber.toString());
}
public static BigDecimal create(Integer doubleNumber) {
return FACTORY.internalCreate(doubleNumber.toString());
}
public static BigDecimal create(Double doubleNumber) {
return FACTORY.internalCreate(doubleNumber.toString());
}
}
Test:
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
public class JavaTeste {
public static void main(String args[]){
//1) With your own BigDecimal
BigDecimal b1 = BigDecimalFactory.create("100");
BigDecimal b2 = BigDecimalFactory.create("25");
BigDecimal b3 = BigDecimalFactory.create("13");
System.out.println(b1.divide(b2));
System.out.println(b1.divide(b3));
//2) Without your own BigDecimal
MathContext mathContext = new MathContext(38, RoundingMode.HALF_UP);
BigDecimal b01 = new BigDecimal("100", mathContext);
BigDecimal b02 = new BigDecimal("25", mathContext);
BigDecimal b03 = new BigDecimal("13", mathContext);
System.out.println(b01.divide(b02));
System.out.println(b01.divide(b03, mathContext));
//3) And this just not work
BigDecimal b001 = new BigDecimal("100");
BigDecimal b002 = new BigDecimal("25");
BigDecimal b003 = new BigDecimal("13");
System.out.println(b001.divide(b002));
System.out.println(b001.divide(b003));
}
}