The BigDecimal class provides two convenience methods for explicit rounding:
roundToDigitPositionAfterDecimalPoint(digitPosition: Long, roundingMode: RoundingMode): Rounds to a specific position relative to the decimal point.roundToDigitPosition(digitPosition: Long, roundingMode: RoundingMode): Rounds to a specific digit precision regardless of the decimal point position.
Examples
// Rounding to 3 positions after the decimal point
val rounded = BigDecimal.fromIntWithExponent(123456789, 3)
.roundToDigitPositionAfterDecimalPoint(3, RoundingMode.CEILING)
// Result: "1234.568"
// Rounding to 3 digits of precision (regardless of decimal point)
val rounded2 = BigDecimal.parseString("1234.5678")
.roundToDigitPosition(3, RoundingMode.ROUND_HALF_TOWARDS_ZERO)
// Result: "1230"
// Rounding to 4 digits of precision
val rounded3 = BigDecimal.parseString("0.0012345678")
.roundToDigitPosition(4, RoundingMode.ROUND_HALF_TOWARDS_ZERO)
// Result: "0.001"
// Example of rounding to a specific position after decimal
val rounded = BigDecimal.fromIntWithExponent(123456789, 3)
.roundToDigitPositionAfterDecimalPoint(3, RoundingMode.CEILING)
// Example of rounding to a specific digit precision
val rounded2 = BigDecimal.parseString("1234.5678")
.roundToDigitPosition(3, RoundingMode.ROUND_HALF_TOWARDS_ZERO)