Compute percentage for bigdecimals

You may want to implement the division by 100 using BigDecimal.scaleByPowerOfTen(-2).

It adds up if you do it a million times. It is much faster in my experience.

There is also a similar method BigDecimal.movePointLeft(2) - see the other thread for details and decide which one works better for you.


I don't think there is an API for that (I never needed it).
Your solution seams good to me, maybe you just add the constant ONE_HUNDRED:

public static final BigDecimal ONE_HUNDRED = new BigDecimal(100);

public static BigDecimal percentage(BigDecimal base, BigDecimal pct){
    return base.multiply(pct).divide(ONE_HUNDRED);
}

probably not that much gain, only if called very often

eventually put it in some Util class...

Tags:

Java