diff --git a/libraries/stdlib/src/kotlin/util/BigNumbers.kt b/libraries/stdlib/src/kotlin/util/BigNumbers.kt index ef7410fdfed..3a205686a96 100644 --- a/libraries/stdlib/src/kotlin/util/BigNumbers.kt +++ b/libraries/stdlib/src/kotlin/util/BigNumbers.kt @@ -5,6 +5,7 @@ package kotlin import java.math.BigDecimal import java.math.BigInteger +import java.math.RoundingMode /** * Enables the use of the `+` operator for [BigInteger] instances. @@ -57,9 +58,12 @@ public inline operator fun BigDecimal.times(other: BigDecimal) : BigDecimal = th /** * Enables the use of the `/` operator for [BigDecimal] instances. + * + * The scale of the result is the same as the scale of `this` (divident), and for rounding the [RoundingMode.HALF_EVEN] + * rounding mode is used. */ @kotlin.internal.InlineOnly -public inline operator fun BigDecimal.div(other: BigDecimal) : BigDecimal = this.divide(other) +public inline operator fun BigDecimal.div(other: BigDecimal) : BigDecimal = this.divide(other, RoundingMode.HALF_EVEN) /** * Enables the use of the `%` operator for [BigDecimal] instances. diff --git a/libraries/stdlib/test/numbers/NumbersJVMTest.kt b/libraries/stdlib/test/numbers/NumbersJVMTest.kt index fedc6545467..adb8f5578e3 100644 --- a/libraries/stdlib/test/numbers/NumbersJVMTest.kt +++ b/libraries/stdlib/test/numbers/NumbersJVMTest.kt @@ -1,5 +1,6 @@ package numbers +import java.math.BigDecimal import org.junit.Test as test import kotlin.test.* @@ -38,4 +39,15 @@ class NumbersJVMTest { assertEquals(java.lang.Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY) assertEquals(java.lang.Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY) } + + @test fun bigDecimalDivRounding() { + val (d1, d2, d3, d4, d5) = (1..5).map { BigDecimal(it.toString()) } + val d7 = BigDecimal("7") + + assertEquals(d1, d2 / d3) + assertEquals(d2, d3 / d2) + assertEquals(d2, d5 / d2) + assertEquals(d4, d7 / d2) + assertEquals(d1, d7 / d5) + } } \ No newline at end of file