Skip to content
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Unit.convert()` and `Unit.getConversionMultiplier(Unit)` now strip any trailing fractional zeros and the
scale of the result is >= 0.

## [7.0.0] - 2025-07-22

### Added
Expand Down
58 changes: 35 additions & 23 deletions qudtlib-model/src/main/java/io/github/qudtlib/model/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,10 +401,16 @@ public BigDecimal convert(BigDecimal value, Unit toUnit, QuantityKind quantityKi
? BigDecimal.ZERO
: toUnit.getConversionOffset().orElse(BigDecimal.ZERO);
BigDecimal toMultiplier = toUnit.getConversionMultiplier().orElse(BigDecimal.ONE);
return value.add(fromOffset)
.multiply(fromMultiplier, MathContext.DECIMAL128)
.divide(toMultiplier, MathContext.DECIMAL128)
.subtract(toOffset);
BigDecimal result =
value.add(fromOffset)
.multiply(fromMultiplier, MathContext.DECIMAL128)
.divide(toMultiplier, MathContext.DECIMAL128)
.subtract(toOffset)
.stripTrailingZeros();
if (result.scale() < 0) {
result = result.setScale(0);
}
return result;
}

/**
Expand All @@ -427,25 +433,31 @@ public BigDecimal getConversionMultiplier(Unit toUnit) {
}
Optional<BigDecimal> fromMultiplier = this.getConversionMultiplier();
Optional<BigDecimal> toMultiplier = toUnit.getConversionMultiplier();
return fromMultiplier
.map(
from ->
toMultiplier
.map(to -> from.divide(to, MathContext.DECIMAL128))
.orElse(null))
.orElseThrow(
() ->
new InconvertibleQuantitiesException(
String.format(
"Cannot convert %s(%s) to %s(%s)",
this.getIriAbbreviated(),
this.getConversionMultiplier().isEmpty()
? "no multiplier"
: "has multiplier",
toUnit.getIriAbbreviated(),
toUnit.getConversionMultiplier().isEmpty()
? "no multiplier"
: "has multiplier")));
BigDecimal result =
fromMultiplier
.map(
from ->
toMultiplier
.map(to -> from.divide(to, MathContext.DECIMAL128))
.orElse(null))
.orElseThrow(
() ->
new InconvertibleQuantitiesException(
String.format(
"Cannot convert %s(%s) to %s(%s)",
this.getIriAbbreviated(),
this.getConversionMultiplier().isEmpty()
? "no multiplier"
: "has multiplier",
toUnit.getIriAbbreviated(),
toUnit.getConversionMultiplier().isEmpty()
? "no multiplier"
: "has multiplier")));
result = result.stripTrailingZeros();
if (result.scale() < 0) {
result = result.setScale(0);
}
return result;
}

public boolean conversionOffsetDiffers(Unit other) {
Expand Down
8 changes: 5 additions & 3 deletions qudtlib-test/src/test/java/io/github/qudtlib/QudtTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,12 @@ public void testMilliSV__PER__HR_to_MicroSV__PER__HR() {
u.getIriLocalname(),
u.getConversionMultiplier().get().toString())));
*/
MatcherAssert.assertThat(
BigDecimal converted =
Qudt.convert(
new BigDecimal("15.12"), Units.MilliSV__PER__HR, Units.MicroSV__PER__HR),
Matchers.comparesEqualTo(new BigDecimal(15120)));
new BigDecimal("15.12"), Units.MilliSV__PER__HR, Units.MicroSV__PER__HR);
MatcherAssert.assertThat(converted, Matchers.comparesEqualTo(new BigDecimal(15120)));
BigDecimal expected = new BigDecimal(15120);
assertEquals(expected, converted);
}

@Test
Expand Down