Skip to content

Commit cc47c7c

Browse files
author
willem.romijn
committed
fix and tests for lost precision in unit conversion
1 parent 7ef28d0 commit cc47c7c

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

measure/src/main/kotlin/com/alliander/open/measure/Measure.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ data class Measure<U : Units>(val amount: BigDecimal, val units: U) : Comparable
4747
infix fun <A : U> `in`(other: A): BigDecimal =
4848
if (units == other)
4949
amount
50-
else (amount * units.ratio).divide(other.ratio, amount.scale() + units.ratio.precision(), RoundingMode.UP)
50+
else (amount * units.ratio).divide(other.ratio, amount.scale() + other.ratio.precision(), RoundingMode.UP)
5151

5252
operator fun plus(other: Measure<U>): Measure<U> = baseUnits(
5353
units,

measure/src/test/kotlin/com/alliander/open/measure/MeasureTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,14 @@ class MeasureTest : StringSpec({
218218

219219
result shouldBe expectedResult
220220
}
221+
222+
"Unit conversion uses the correct scale" {
223+
val energyInJoule = 13500000 * joule
224+
val valueInKwh = energyInJoule `in` kiloWattHour
225+
valueInKwh.stripTrailingZeros() shouldBe BigDecimal.valueOf(3.75)
226+
227+
val powerInW = 1.99E7 * watt
228+
val valueInMw = powerInW `as` megaWatt
229+
valueInMw.amount.stripTrailingZeros() shouldBe BigDecimal.valueOf(19.9)
230+
}
221231
})

0 commit comments

Comments
 (0)