Lluis Martinez Lluis Martinez - 1 year ago 172
Java Question

Compute percentage for bigdecimals

I haven't found any native method to do this, so I created my own in a helper class:

public static BigDecimal percentage(BigDecimal base, BigDecimal pct){
return base.multiply(pct).divide(new BigDecimal(100));

But I don't quite like it, I wonder if the API has something similar. The Number class (ancestor of BigDecimal) would be a nice place.

Answer Source

Feel free to subclass BigDecimal and add that method. Beyond that, what do you expect? You know where to find the API and confirm that the class you would like to have that method doesn't. Personally, I'd say the functionality is so trivial that there wouldn't be much of a point in having it in the standard API.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download