From 64fc4a44d04e3bb58f5489afbec7c6d84e66953f Mon Sep 17 00:00:00 2001 From: Scott Russell Date: Sat, 30 Aug 2014 23:12:15 +0000 Subject: [PATCH] ICU-10970 Support decimal point required X-SVN-Rev: 36301 --- .../src/com/ibm/icu/text/DecimalFormat.java | 35 ++++++++++++++++++ .../icu/dev/test/format/NumberFormatTest.java | 37 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java index 7abba4e40ae..d5aaa05d163 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java @@ -2305,6 +2305,8 @@ public class DecimalFormat extends NumberFormat { "com.ibm.icu.text.DecimalFormat.SkipExtendedSeparatorParsing", "false") .equals("true"); + // allow control of requiring a matching decimal point when parsing + boolean parseRequireDecimalPoint = false; // When parsing a number with big exponential value, it requires to transform the // value into a string representation to construct BigInteger instance. We want to @@ -2628,6 +2630,14 @@ public class DecimalFormat extends NumberFormat { } } + if(digits.decimalAt == 0 && isDecimalPatternMatchRequired()) { + if(this.formatPattern.indexOf(decimal) != -1) { + parsePosition.setIndex(oldStart); + parsePosition.setErrorIndex(position); + return false; + } + } + if (backup != -1) position = backup; @@ -3766,6 +3776,31 @@ public class DecimalFormat extends NumberFormat { public boolean isDecimalSeparatorAlwaysShown() { return decimalSeparatorAlwaysShown; } + + /** + * When decimal match is not required, the input does not have to + * contain a decimal mark when there is a decimal mark specified in the + * pattern. + * @param value true if input must contain a match to decimal mark in pattern + * Default is false. + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public void setDecimalPatternMatchRequired(boolean value) { + parseRequireDecimalPoint = value; + } + + /** + * {@icu} Returns whether the input to parsing must contain a decimal mark if there + * is a decimal mark in the pattern. + * @return true if input must contain a match to decimal mark in pattern + * @draft ICU 54 + * @provisional This API might change or be removed in a future release. + */ + public boolean isDecimalPatternMatchRequired() { + return parseRequireDecimalPoint; + } + /** * Sets the behavior of the decimal separator with integers. (The decimal separator diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index df67d0499b6..8db671984b9 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -3682,4 +3682,41 @@ public class NumberFormatTest extends com.ibm.icu.dev.test.TestFmwk { assertEquals("Test Currency Context", TWD_changed_expected, TWD_changed); } } + + public void TestParseRequiredDecimalPoint() { + + String[] testPattern = { "00.####", "00.0", "00" }; + + String value2Parse = "99"; + double parseValue = 99; + DecimalFormat parser = new DecimalFormat(); + double result; + boolean hasDecimalPoint; + for (int i = 0; i < testPattern.length; i++) { + parser.applyPattern(testPattern[i]); + hasDecimalPoint = testPattern[i].contains("."); + + parser.setDecimalPatternMatchRequired(false); + try { + result = parser.parse(value2Parse).doubleValue(); + assertEquals("wrong parsed value", parseValue, result); + } catch (ParseException e) { + this.errln("Parsing " + value2Parse + " should have succeeded with " + testPattern[i] + + " and isDecimalPointMatchRequired set to: " + parser.isDecimalPatternMatchRequired()); + } + + parser.setDecimalPatternMatchRequired(true); + try { + result = parser.parse(value2Parse).doubleValue(); + if(hasDecimalPoint){ + this.errln("Parsing " + value2Parse + " should NOT have succeeded with " + testPattern[i] + + " and isDecimalPointMatchRequired set to: " + parser.isDecimalPatternMatchRequired()); + } + } catch (ParseException e) { + // OK, should fail + } + } + + } + } -- 2.40.0