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

ICU-22781 Add support for converting units with constant denominators (Java) #3346

Merged
merged 1 commit into from
Jan 24, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public UnitsConverter.Factor getFactorToBase(MeasureUnitImpl measureUnit) {
result = result.multiply(getFactorToBase(singleUnit));
}

if (measureUnit.getConstantDenominator() != 0) {
result = result.divide(BigDecimal.valueOf(measureUnit.getConstantDenominator()));
}

return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,12 @@ public Factor multiply(Factor other) {
return result;
}

public Factor divide(BigDecimal value) {
Factor result = this.copy();
result.factorDen = this.factorDen.multiply(value);
return result;
}

/**
* Adds Entity with power or not. For example, {@code 12 ^ 3} or {@code 12}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,25 @@ class TestData {
new TestData("dot-per-centimeter", "pixel-per-centimeter", 1.0, 1.0),
new TestData("dot-per-inch", "pixel-per-inch", 1.0, 1.0),
new TestData("dot", "pixel", 1.0, 1.0),

// With constants
new TestData("meter-per-10", "foot", 1.0, 0.328084),
new TestData("meter", "foot-per-10", 1.0, 32.8084),
new TestData("meter", "foot-per-100", 1.0, 328.084),
new TestData("portion", "portion-per-1000", 1.0, 1000),
new TestData("portion", "portion-per-10000", 1.0, 10000),
new TestData("portion", "portion-per-100000", 1.0, 100000),
new TestData("portion", "portion-per-1000000", 1.0, 1000000),
new TestData("portion-per-10", "portion", 1.0, 0.1),
new TestData("portion-per-100", "portion", 1.0, 0.01),
new TestData("portion-per-1000", "portion", 1.0, 0.001),
new TestData("portion-per-10000", "portion", 1.0, 0.0001),
new TestData("portion-per-100000", "portion", 1.0, 0.00001),
new TestData("portion-per-1000000", "portion", 1.0, 0.000001),
new TestData("mile-per-hour", "meter-per-second", 1.0, 0.44704),
new TestData("mile-per-100-hour", "meter-per-100-second", 1.0, 0.44704),
new TestData("mile-per-hour", "meter-per-100-second", 1.0, 44.704),
new TestData("mile-per-100-hour", "meter-per-second", 1.0, 0.0044704),
};

ConversionRates conversionRates = new ConversionRates();
Expand Down