Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed scale issue in conversion function #21

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions measure/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ plugins {
// Apply the java-library plugin for API and implementation separation.
`java-library`

// Apply dokka plugin to allow extraction of ducumentation from KDoc comments
id("org.jetbrains.dokka") version "1.4.20"
// Apply dokka plugin to allow extraction of documentation from KDoc comments
id("org.jetbrains.dokka") version "1.4.32"

// Make sure we can publish to maven
`maven-publish`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,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,10 @@ 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)
}
})
Loading