Skip to content

Commit

Permalink
CLDR-17298 Add parts per billion (#3716)
Browse files Browse the repository at this point in the history
  • Loading branch information
macchiati authored May 14, 2024
1 parent 903f21c commit 9112c2e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 2 deletions.
15 changes: 15 additions & 0 deletions common/main/en.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6886,6 +6886,11 @@ annotations.
<unitPattern count="one">{0} part per million</unitPattern>
<unitPattern count="other">{0} parts per million</unitPattern>
</unit>
<unit type="concentr-portion-per-1e9">
<displayName>parts per billion</displayName>
<unitPattern count="one">{0} part per billion</unitPattern>
<unitPattern count="other">{0} parts per billion</unitPattern>
</unit>
<unit type="concentr-percent">
<displayName>percent</displayName>
<unitPattern count="one">{0} percent</unitPattern>
Expand Down Expand Up @@ -7970,6 +7975,11 @@ annotations.
<unitPattern count="one">{0} ppm</unitPattern>
<unitPattern count="other">{0} ppm</unitPattern>
</unit>
<unit type="concentr-portion-per-1e9">
<displayName>parts/billion</displayName>
<unitPattern count="one">{0} ppb</unitPattern>
<unitPattern count="other">{0} ppb</unitPattern>
</unit>
<unit type="concentr-percent">
<displayName>percent</displayName>
<unitPattern count="one">{0}%</unitPattern>
Expand Down Expand Up @@ -9051,6 +9061,11 @@ annotations.
<unitPattern count="one">{0}ppm</unitPattern>
<unitPattern count="other">{0}ppm</unitPattern>
</unit>
<unit type="concentr-portion-per-1e9">
<displayName>ppb</displayName>
<unitPattern count="one">{0}ppb</unitPattern>
<unitPattern count="other">{0}ppb</unitPattern>
</unit>
<unit type="concentr-percent">
<displayName>%</displayName>
<unitPattern count="one">{0}%</unitPattern>
Expand Down
4 changes: 4 additions & 0 deletions common/main/root.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5101,6 +5101,10 @@ Warnings: All cp values have U+FE0F characters removed. See /annotationsDerived/
<displayName>ppm</displayName>
<unitPattern count="other">{0} ppm</unitPattern>
</unit>
<unit type="concentr-portion-per-1e9">
<displayName>ppb</displayName>
<unitPattern count="other">{0} ppb</unitPattern>
</unit>
<unit type="concentr-percent">
<displayName>%</displayName>
<unitPattern count="other">{0}%</unitPattern>
Expand Down
1 change: 1 addition & 0 deletions common/validity/unit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ For terms of use, see http://www.unicode.org/copyright.html
volume-sai
volume-to-jp
volume-koku
concentr-portion-per-1e9
mass-fun
duration-night
speed-light-speed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,7 @@ private static final class UnitOrderHolder {
"volume-koku",
"speed-light-speed",
"mass-fun",
"concentr-portion-per-1e9",
"duration-night"))
.freeze();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ public int compare(String o1, String o2) {
}

Comparator<String> UNIT_COMPARATOR = new UnitComparator();
static final Pattern TRAILING_ZEROS = Pattern.compile("0+$");

/** Only handles the canonical units; no kilo-, only normalized, etc. */
// Thus we do not need to handle specials here
Expand Down Expand Up @@ -1174,7 +1175,7 @@ public String toString() {
for (int i = 1; i >= 0; --i) { // two passes, numerator then den.
boolean positivePass = i > 0;
if (positivePass && !factor.numerator.equals(BigInteger.ONE)) {
builder.append(factor.numerator);
builder.append(shortConstant(factor.numerator));
}

Map<String, Integer> target = positivePass ? numUnitsToPowers : denUnitsToPowers;
Expand All @@ -1190,7 +1191,7 @@ public String toString() {
firstDenominator = false;
builder.append("per-");
if (!factor.denominator.equals(BigInteger.ONE)) {
builder.append(factor.denominator).append('-');
builder.append(shortConstant(factor.denominator)).append('-');
}
}
}
Expand All @@ -1213,10 +1214,35 @@ public String toString() {
}
builder.append(unit);
}
if (!positivePass
&& firstDenominator
&& !factor.denominator.equals(BigInteger.ONE)) {
builder.append("-per-").append(shortConstant(factor.denominator));
}
}
return builder.toString();
}

/**
* Return a string format. If larger than 7 digits, use 1eN format.
*
* @param source
* @return
*/
public String shortConstant(BigInteger source) {
// don't bother optimizing
String result = source.toString();
if (result.length() < 8) {
return result;
}
Matcher matcher = TRAILING_ZEROS.matcher(result);
if (matcher.find()) {
int zeroCount = matcher.group().length();
return result.substring(0, result.length() - zeroCount) + "e" + zeroCount;
}
return result;
}

public String toString(
LocaleStringProvider resolvedFile,
String width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3197,6 +3197,10 @@ public void TestUnitOrder() {

checkNormalization("test case", "newton-meter");
checkNormalization("test case", "acre-foot");
checkNormalization("test case", "portion-per-1e9");
checkNormalization("test case", "portion-per-1000");
checkNormalization("test case", "1e9-meter");
checkNormalization("test case", "1000-meter");

String stdAcre = converter.getStandardUnit("acre");

Expand Down

0 comments on commit 9112c2e

Please sign in to comment.