Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ data class Measure<U : Units>(val amount: BigDecimal, val units: U) : Comparable
infix fun <A : U> `in`(other: A): BigDecimal =
if (units == other)
amount
else (amount * units.ratio).divide(other.ratio, amount.scale() + units.ratio.precision(), RoundingMode.UP)
else (amount * units.ratio).divide(other.ratio, amount.scale() + other.ratio.precision(), RoundingMode.UP)

operator fun plus(other: Measure<U>): Measure<U> = baseUnits(
units,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,18 @@ class MeasureTest : StringSpec({

result shouldBe expectedResult
}

"Unit conversion uses the correct scale" {
val energyInJoule = 13500000 * joule
val valueInKwh = energyInJoule `in` kiloWattHour
valueInKwh.stripTrailingZeros() shouldBe BigDecimal.valueOf(3.75)

val energyInJoule2 = 3.75 * kiloWattHour
val valueInKwh2 = energyInJoule2 `in` joule
valueInKwh2.stripTrailingZeros() shouldBe BigDecimal.valueOf(1.35E+7)

val powerInW = 1.99E7 * watt
val valueInMw = powerInW `as` megaWatt
valueInMw.amount.stripTrailingZeros() shouldBe BigDecimal.valueOf(19.9)
}
})
Loading