Rounding BigDecimal to *always* have two decimal places

I’m trying to round BigDecimal values up, to two decimal places. I’m using BigDecimal rounded = value.round(new MathContext(2, RoundingMode.CEILING)); logger.trace(“rounded {} to {}”, value, rounded); but it doesn’t do what I want consistently: rounded 0.819 to 0.82 rounded 1.092 to 1.1 rounded 1.365 to 1.4 // should be 1.37 rounded 2.730 to 2.8 // should … Read more

Why does .NET use banker’s rounding as default?

According to the documentation, the decimal.Round method uses a round-to-even algorithm which is not common for most applications. So I always end up writing a custom function to do the more natural round-half-up algorithm: public static decimal RoundHalfUp(this decimal d, int decimals) { if (decimals < 0) { throw new ArgumentException(“The decimals must be non-negative”, … Read more