Use HALF_EVEN rounding mode by default for BigDecimal division operator.

#KT-10462 Fixed
This commit is contained in:
Ilya Gorbunov
2016-02-08 22:12:20 +03:00
parent 8b5e9d1685
commit 6c7cefaae3
2 changed files with 17 additions and 1 deletions
@@ -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.
@@ -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)
}
}