From: Travis Keep Date: Fri, 13 Jun 2014 20:18:57 +0000 (+0000) Subject: ICU-10837 Andy & Mark's review. X-Git-Tag: milestone-59-0-1~1850 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=04804b878fb8e919d6e7ce0fed766d42cf60a73b;p=icu ICU-10837 Andy & Mark's review. X-SVN-Rev: 35878 --- diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java index fac38e17183..5785af96f89 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormatSymbols.java @@ -544,6 +544,24 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { public void setMonetaryGroupingSeparator(char sep) { monetaryGroupingSeparator = sep; } + + /** + * Returns the multiplication sign + * @draft ICU 54 + * @provisional + */ + public String getExponentMultiplicationSign() { + return exponentMultiplicationSign; + } + + /** + * Sets the multiplication sign + * @draft ICU 54 + * @provisional + */ + public void setExponentMultiplicationSign(String exponentMultiplicationSign) { + this.exponentMultiplicationSign = exponentMultiplicationSign; + } /** * {@icu} Returns the string used to separate the mantissa from the exponent. @@ -803,7 +821,8 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { plusString.equals(other.plusString) && exponentSeparator.equals(other.exponentSeparator) && monetarySeparator == other.monetarySeparator && - monetaryGroupingSeparator == other.monetaryGroupingSeparator); + monetaryGroupingSeparator == other.monetaryGroupingSeparator && + exponentMultiplicationSign.equals(other.exponentMultiplicationSign)); } /** @@ -875,7 +894,7 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { boolean isLatn = nsName.equals("latn"); String baseKey = "NumberElements/" + nsName + "/symbols/"; String latnKey = "NumberElements/latn/symbols/"; - String[] symbolKeys = { "decimal", "group", "list", "percentSign", "minusSign", "plusSign", "exponential", "perMille", "infinity", "nan", "currencyDecimal", "currencyGroup" }; + String[] symbolKeys = { "decimal", "group", "list", "percentSign", "minusSign", "plusSign", "exponential", "perMille", "infinity", "nan", "currencyDecimal", "currencyGroup", "superscriptingExponent" }; String[] fallbackElements = { ".", ",", ";", "%", "-", "+", "E", "\u2030", "\u221e", "NaN", null, null }; String[] symbolsArray = new String[symbolKeys.length]; for ( int i = 0 ; i < symbolKeys.length; i++ ) { @@ -933,6 +952,12 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { monetaryGroupingSeparator = groupingSeparator; } + if ( numberElements[12] != null) { + exponentMultiplicationSign = numberElements[12]; + } else { + exponentMultiplicationSign = "\u00D7"; + } + digit = DecimalFormat.PATTERN_DIGIT; // Localized pattern character no longer in CLDR padEscape = DecimalFormat.PATTERN_PAD_ESCAPE; sigDigit = DecimalFormat.PATTERN_SIGNIFICANT_DIGIT; @@ -1051,6 +1076,11 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { plusString = new String(plusArray); } } + if (serialVersionOnStream < 8) { + if (exponentMultiplicationSign == null) { + exponentMultiplicationSign = "\u00D7"; + } + } serialVersionOnStream = currentSerialVersion; // recreate @@ -1234,6 +1264,13 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { */ private String minusString = null; private String plusString = null; + + /** + * Exponent multiplication sign. e.g "x" + * @serial + * @since ICU 54 + */ + private String exponentMultiplicationSign = null; // Proclaim JDK 1.1 FCS compatibility private static final long serialVersionUID = 5772796243397350300L; @@ -1249,7 +1286,8 @@ public class DecimalFormatSymbols implements Cloneable, Serializable { // - 5 for ICU 3.6, which includes the monetaryGroupingSeparator field // - 6 for ICU 4.2, which includes the currencySpc* fields // - 7 for ICU 52, which includes the minusString and plusString fields - private static final int currentSerialVersion = 7; + // - 8 for ICU 54, which includes exponentMultiplicationSign field. + private static final int currentSerialVersion = 8; /** * Describes the version of DecimalFormatSymbols present on the stream. diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/ScientificFormatHelper.java b/icu4j/main/classes/core/src/com/ibm/icu/text/ScientificFormatHelper.java index 9b326b6069b..bccf8f3f925 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/ScientificFormatHelper.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/ScientificFormatHelper.java @@ -66,8 +66,7 @@ public final class ScientificFormatHelper { } private static String getMultiplicationSymbol(DecimalFormatSymbols dfs) { - //TODO: revisit this - return "\u00d7"; + return dfs.getExponentMultiplicationSign(); } /** @@ -86,12 +85,15 @@ public final class ScientificFormatHelper { CharSequence endMarkup) { int copyFromOffset = 0; StringBuilder result = new StringBuilder(); + boolean exponentSymbolFieldPresent = false; + boolean exponentFieldPresent = false; for ( iterator.first(); iterator.current() != CharacterIterator.DONE; ) { Map attributeSet = iterator.getAttributes(); if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) { + exponentSymbolFieldPresent = true; append( iterator, copyFromOffset, @@ -102,6 +104,7 @@ public final class ScientificFormatHelper { result.append(preExponent); result.append(beginMarkup); } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) { + exponentFieldPresent = true; int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT); append( iterator, @@ -114,7 +117,10 @@ public final class ScientificFormatHelper { } else { iterator.next(); } - } + } + if (!exponentSymbolFieldPresent || !exponentFieldPresent) { + throw new IllegalArgumentException("Must start with standard e notation."); + } append(iterator, copyFromOffset, iterator.getEndIndex(), result); return result.toString(); } @@ -146,12 +152,15 @@ public final class ScientificFormatHelper { public String toSuperscriptExponentDigits(AttributedCharacterIterator iterator) { int copyFromOffset = 0; StringBuilder result = new StringBuilder(); + boolean exponentSymbolFieldPresent = false; + boolean exponentFieldPresent = false; for ( iterator.first(); iterator.current() != CharacterIterator.DONE; ) { Map attributeSet = iterator.getAttributes(); if (attributeSet.containsKey(NumberFormat.Field.EXPONENT_SYMBOL)) { + exponentSymbolFieldPresent = true; append( iterator, copyFromOffset, @@ -184,6 +193,7 @@ public final class ScientificFormatHelper { copyFromOffset = limit; iterator.setIndex(copyFromOffset); } else if (attributeSet.containsKey(NumberFormat.Field.EXPONENT)) { + exponentFieldPresent = true; int start = iterator.getRunStart(NumberFormat.Field.EXPONENT); int limit = iterator.getRunLimit(NumberFormat.Field.EXPONENT); append( @@ -198,6 +208,9 @@ public final class ScientificFormatHelper { iterator.next(); } } + if (!exponentSymbolFieldPresent || !exponentFieldPresent) { + throw new IllegalArgumentException("Must start with standard e notation."); + } append(iterator, copyFromOffset, iterator.getEndIndex(), result); return result.toString(); } diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ScientificFormatHelperTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ScientificFormatHelperTest.java index 3a75c80d9cb..22ee42a1218 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ScientificFormatHelperTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/ScientificFormatHelperTest.java @@ -35,8 +35,22 @@ public class ScientificFormatHelperTest extends TestFmwk { "1.23456\u00d710\u207b\u2077\u2078", helper.toSuperscriptExponentDigits(iterator)); } + + public void TestPlusSignInExponentMarkup() { + ULocale en = new ULocale("en"); + DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(en); + decfmt.applyPattern("0.00E+0"); + AttributedCharacterIterator iterator = decfmt.formatToCharacterIterator(6.02e23); + ScientificFormatHelper helper = ScientificFormatHelper.getInstance( + decfmt.getDecimalFormatSymbols()); + assertEquals( + "", + "6.02\u00d710+23", + helper.insertMarkup(iterator, "", "")); + } + - public void TestPlusSignInExponent() { + public void TestPlusSignInExponentSuperscript() { ULocale en = new ULocale("en"); DecimalFormat decfmt = (DecimalFormat) NumberFormat.getScientificInstance(en); decfmt.applyPattern("0.00E+0"); @@ -48,4 +62,32 @@ public class ScientificFormatHelperTest extends TestFmwk { "6.02\u00d710\u207a\u00b2\u00b3", helper.toSuperscriptExponentDigits(iterator)); } + + public void TestFixedDecimalMarkup() { + ULocale en = new ULocale("en"); + DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(en); + AttributedCharacterIterator iterator = decfmt.formatToCharacterIterator(123456.0); + ScientificFormatHelper helper = ScientificFormatHelper.getInstance( + decfmt.getDecimalFormatSymbols()); + try { + helper.insertMarkup(iterator, "", ""); + fail("expected illegal argument exception"); + } catch (IllegalArgumentException expected) { + // do nothing + } + } + + public void TestFixedDecimalSuperscript() { + ULocale en = new ULocale("en"); + DecimalFormat decfmt = (DecimalFormat) NumberFormat.getInstance(en); + AttributedCharacterIterator iterator = decfmt.formatToCharacterIterator(123456.0); + ScientificFormatHelper helper = ScientificFormatHelper.getInstance( + decfmt.getDecimalFormatSymbols()); + try { + helper.toSuperscriptExponentDigits(iterator); + fail("expected illegal argument exception"); + } catch (IllegalArgumentException expected) { + // do nothing + } + } } diff --git a/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/DecimalFormatSymbolsTest.java b/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/DecimalFormatSymbolsTest.java index 5b83442b96d..b32a1528b06 100644 --- a/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/DecimalFormatSymbolsTest.java +++ b/icu4j/main/tests/localespi/src/com/ibm/icu/dev/test/localespi/DecimalFormatSymbolsTest.java @@ -1,6 +1,6 @@ /* ******************************************************************************* - * Copyright (C) 2008-2012, International Business Machines Corporation and * + * Copyright (C) 2008-2014, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ @@ -132,6 +132,7 @@ public class DecimalFormatSymbolsTest extends TestFmwk { decfs.setPercent(decfsEnUS.getPercent()); decfs.setPerMill(decfsEnUS.getPerMill()); decfs.setZeroDigit(decfsEnUS.getZeroDigit()); + decfs.setExponentMultiplicationSign(decfsEnUS.getExponentMultiplicationSign()); // Check Currency cur = decfs.getCurrency(); @@ -156,6 +157,7 @@ public class DecimalFormatSymbolsTest extends TestFmwk { checkEquivalence(decfs.getPercent(), decfsEnUS.getPercent(), loc, "getPercent"); checkEquivalence(decfs.getPerMill(), decfsEnUS.getPerMill(), loc, "getPerMill"); checkEquivalence(decfs.getZeroDigit(), decfsEnUS.getZeroDigit(), loc, "getZeroDigit"); + checkEquivalence(decfs.getExponentMultiplicationSign(), decfsEnUS.getExponentMultiplicationSign(), loc, "getExponentMultiplicationSign"); } public void TestKeywords() {