From 9112c2e31a9131de2e8b466c24fe9ec7c22cfa4b Mon Sep 17 00:00:00 2001 From: Mark Davis Date: Tue, 14 May 2024 16:17:42 -0600 Subject: [PATCH] CLDR-17298 Add parts per billion (#3716) --- common/main/en.xml | 15 ++++++++++ common/main/root.xml | 4 +++ common/validity/unit.xml | 1 + .../java/org/unicode/cldr/util/DtdData.java | 1 + .../org/unicode/cldr/util/UnitConverter.java | 30 +++++++++++++++++-- .../org/unicode/cldr/unittest/TestUnits.java | 4 +++ 6 files changed, 53 insertions(+), 2 deletions(-) diff --git a/common/main/en.xml b/common/main/en.xml index 9b9bef60494..2a707679462 100644 --- a/common/main/en.xml +++ b/common/main/en.xml @@ -6886,6 +6886,11 @@ annotations. {0} part per million {0} parts per million + + parts per billion + {0} part per billion + {0} parts per billion + percent {0} percent @@ -7970,6 +7975,11 @@ annotations. {0} ppm {0} ppm + + parts/billion + {0} ppb + {0} ppb + percent {0}% @@ -9051,6 +9061,11 @@ annotations. {0}ppm {0}ppm + + ppb + {0}ppb + {0}ppb + % {0}% diff --git a/common/main/root.xml b/common/main/root.xml index ce7e2da7d37..94e6ca56f6a 100644 --- a/common/main/root.xml +++ b/common/main/root.xml @@ -5101,6 +5101,10 @@ Warnings: All cp values have U+FE0F characters removed. See /annotationsDerived/ ppm {0} ppm + + ppb + {0} ppb + % {0}% diff --git a/common/validity/unit.xml b/common/validity/unit.xml index a49f1538ee5..4ed3399dcfd 100644 --- a/common/validity/unit.xml +++ b/common/validity/unit.xml @@ -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 diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/util/DtdData.java b/tools/cldr-code/src/main/java/org/unicode/cldr/util/DtdData.java index 6b842bd4787..7e656dceb24 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/util/DtdData.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/util/DtdData.java @@ -1871,6 +1871,7 @@ private static final class UnitOrderHolder { "volume-koku", "speed-light-speed", "mass-fun", + "concentr-portion-per-1e9", "duration-night")) .freeze(); } diff --git a/tools/cldr-code/src/main/java/org/unicode/cldr/util/UnitConverter.java b/tools/cldr-code/src/main/java/org/unicode/cldr/util/UnitConverter.java index 9da77ae9364..44620cfbb13 100644 --- a/tools/cldr-code/src/main/java/org/unicode/cldr/util/UnitConverter.java +++ b/tools/cldr-code/src/main/java/org/unicode/cldr/util/UnitConverter.java @@ -1073,6 +1073,7 @@ public int compare(String o1, String o2) { } Comparator 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 @@ -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 target = positivePass ? numUnitsToPowers : denUnitsToPowers; @@ -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('-'); } } } @@ -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, diff --git a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestUnits.java b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestUnits.java index 8c3657689d3..ea84b5dd502 100644 --- a/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestUnits.java +++ b/tools/cldr-code/src/test/java/org/unicode/cldr/unittest/TestUnits.java @@ -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");