ICU-10970 Support decimal point required
X-SVN-Rev: 36301
This commit is contained in:
parent
867e55f0cb
commit
64fc4a44d0
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user