yat0
yat0

Reputation: 967

Creating a custom BigDecimal type

In my application, all BigDecimal numbers are scaled to have two decimal places.. In other words, everytime I create a new BigDecimal in my code, I need to use the method scale too:

BigDecimal x = BigDecimal.ZERO;
x.setScale(2, RoundingMode.HALF_UP);

So, to minimize the work, I wanted to create my custom BigDecimal type, something like:

public class CustomBigDecimal extends BigDecimal {

    public CustomBigDecimal(String val) {
        super(val);
        this.setScale(2, RoundingMode.HALF_UP);
    }

}

I know this.setScale(2, RoundingMode.HALF_UP); doesn't do the job, but I can't find the way to do it, is it possible?

Upvotes: 17

Views: 4032

Answers (4)

Fritz Duchardt
Fritz Duchardt

Reputation: 11860

Just to be different and utilize Java 8 - you could also go lambda on the problem:

public static Function<String, BigDecimal> SCALED_BIG_DECIMAL = (val) -> new BigDecimal(val).setScale(2, RoundingMode.HALF_UP);
...

// and this is how you would use it
SCALED_BIG_DECIMAL.apply("20.2343425")

Upvotes: 1

fps
fps

Reputation: 34450

You could create a CustomBigDecimal that extends from BigDecimal. However, as BigDecimal is immutable, you would never inherit state (such as the scale and rounding mode) from the parent class.

I'd go for the utility class suggested in another answer, or maybe a wrapper that delegates every operation to an actual BigDecimal instance. The downside of this approach is that your brand new CustomBigDecimal wouldn't be a BigDecimal, so they wouldn't be interchangeable.

EDIT: a downside of this approach is that you have to delegate about 50 methods. Not the end of the world with a good IDE, but definitely not very appealing...

If, after all, you still want to make CustomBigDecimal inherit from BigDecimal, you'd need to use a decorator approach:

public class CustomBigDecimal extends BigDecimal {

    private final BigDecimal value;

    private CustomBigDecimal(BigDecimal value) {
        super(value.toPlainString()); // needed to compile, 
                                      // useless except for implicit null-check
        this.value = value;
    }

    public CustomBigDecimal(String val) {
        this(new BigDecimal(val).setScale(2, RoundingMode.HALF_UP));
    }

    @Override
    public CustomBigDecimal abs() {
        return new CustomBigDecimal(this.value.abs());
    }

    // TODO all other methods

}

Upvotes: 10

Danilo Gomes
Danilo Gomes

Reputation: 582

You can store a singleton instance of a MathContext, for instance, and use them as argument to BigDecimal Constructor.

Usually you could do:

MathContext context = new MathContext(2, RoundingMode.HALF_UP);
BigDecimal number = new BigDecimal(val, context);

So, if you have a MathContext as singleton, you can do:

BigDecimal number = new BigDecimal(val, MathInstanceHolder.getMathContext());

Where MathInstanceHolder contains a MathContext instance.

Upvotes: 8

Marcus Widegren
Marcus Widegren

Reputation: 540

You could simply create a method for yourself that creates a BigDecimal with zero. Something like:

public static BigDecimal scaled(String val) {
    BigDecimal x = new BigDecimal(val);
    return x.setScale(2, RoundingMode.HALF_UP);
}

Put it in a helper class, like BigDecimalHelper, BigDecimalFactory or whatever. :)

EDIT: Changed it slightly to return the results of setScale, since BigDecimal is immutable. And to further answer the original question: no what you've written is not possible since the state of the object is not changed with setScale().

Upvotes: 16

Related Questions