void DecimalFormat::setGroupingUsed(UBool enabled) {
NumberFormat::setGroupingUsed(enabled); // to set field for compatibility
- if (enabled) {
- // Set to a reasonable default value
- fProperties->groupingSize = 3;
- fProperties->secondaryGroupingSize = -1;
- } else {
- fProperties->groupingSize = 0;
- fProperties->secondaryGroupingSize = 0;
- }
+ fProperties->groupingUsed = enabled;
refreshFormatterNoError();
}
refreshFormatter(status);
}
-DecimalFormat::DecimalFormat(const DecimalFormat& source) {
+DecimalFormat::DecimalFormat(const DecimalFormat& source) : NumberFormat(source) {
fProperties.adoptInstead(new DecimalFormatProperties(*source.fProperties));
fExportedProperties.adoptInstead(new DecimalFormatProperties());
fWarehouse.adoptInstead(new DecimalFormatWarehouse());
// parseCurrency method (backwards compatibility)
int32_t startIndex = parsePosition.getIndex();
fParser->parse(text, startIndex, true, result, status);
+ // TODO: Do we need to check for fProperties->parseAllInput (UCONFIG_HAVE_PARSEALLINPUT) here?
if (result.success()) {
parsePosition.setIndex(result.charEnd);
result.populateFormattable(output);
// parseCurrency method (backwards compatibility)
int32_t startIndex = parsePosition.getIndex();
fParserWithCurrency->parse(text, startIndex, true, result, status);
+ // TODO: Do we need to check for fProperties->parseAllInput (UCONFIG_HAVE_PARSEALLINPUT) here?
if (result.success()) {
parsePosition.setIndex(result.charEnd);
Formattable formattable;
}
int32_t DecimalFormat::getGroupingSize(void) const {
+ if (fProperties->groupingSize < 0) {
+ return 0;
+ }
return fProperties->groupingSize;
}
NumberFormat::setMinimumIntegerDigits(fExportedProperties->minimumIntegerDigits);
NumberFormat::setMaximumFractionDigits(fExportedProperties->maximumFractionDigits);
NumberFormat::setMinimumFractionDigits(fExportedProperties->minimumFractionDigits);
+ // fProperties, not fExportedProperties, since this information comes from the pattern:
+ NumberFormat::setGroupingUsed(fProperties->groupingUsed);
}
void DecimalFormat::refreshFormatterNoError() {
exponentSignAlwaysShown = false;
formatWidth = -1;
groupingSize = -1;
+ groupingUsed = false;
magnitudeMultiplier = 0;
maximumFractionDigits = -1;
maximumIntegerDigits = -1;
eq = eq && exponentSignAlwaysShown == other.exponentSignAlwaysShown;
eq = eq && formatWidth == other.formatWidth;
eq = eq && groupingSize == other.groupingSize;
+ eq = eq && groupingUsed == other.groupingUsed;
eq = eq && magnitudeMultiplier == other.magnitudeMultiplier;
eq = eq && maximumFractionDigits == other.maximumFractionDigits;
eq = eq && maximumIntegerDigits == other.maximumIntegerDigits;
bool exponentSignAlwaysShown;
int32_t formatWidth;
int32_t groupingSize;
+ bool groupingUsed;
int32_t magnitudeMultiplier;
int32_t maximumFractionDigits;
int32_t maximumIntegerDigits;
}
Grouper Grouper::forProperties(const DecimalFormatProperties& properties) {
+ if (!properties.groupingUsed) {
+ return forStrategy(UNUM_GROUPING_OFF);
+ }
auto grouping1 = static_cast<int16_t>(properties.groupingSize);
auto grouping2 = static_cast<int16_t>(properties.secondaryGroupingSize);
auto minGrouping = static_cast<int16_t>(properties.minimumGroupingDigits);
auto grouping3 = static_cast<int16_t> ((positive.groupingSizes >> 32) & 0xffff);
if (grouping2 != -1) {
properties.groupingSize = grouping1;
+ properties.groupingUsed = true;
} else {
properties.groupingSize = -1;
+ properties.groupingUsed = false;
}
if (grouping3 != -1) {
properties.secondaryGroupingSize = grouping2;
// bug 10864
status = U_ZERO_ERROR;
DecimalFormat noGrouping("###0.##", status);
- if (noGrouping.getGroupingSize() != 0) {
- errln("Grouping size should be 0 for no grouping.");
- }
+ assertEquals("Grouping size should be 0 for no grouping.", 0, noGrouping.getGroupingSize());
noGrouping.setGroupingUsed(TRUE);
- if (noGrouping.getGroupingSize() != 0) {
- errln("Grouping size should still be 0.");
- }
+ assertEquals("Grouping size should still be 0.", 0, noGrouping.getGroupingSize());
// end bug 10864
+ // bug 13442 comment 14
+ status = U_ZERO_ERROR;
+ {
+ DecimalFormat df("0", {"en", status}, status);
+ UnicodeString result;
+ assertEquals("pat 0: ", 0, df.getGroupingSize());
+ assertEquals("pat 0: ", (UBool) FALSE, (UBool) df.isGroupingUsed());
+ df.setGroupingUsed(false);
+ assertEquals("pat 0 then disabled: ", 0, df.getGroupingSize());
+ assertEquals("pat 0 then disabled: ", u"1111", df.format(1111, result.remove()));
+ df.setGroupingUsed(true);
+ assertEquals("pat 0 then enabled: ", 0, df.getGroupingSize());
+ assertEquals("pat 0 then enabled: ", u"1111", df.format(1111, result.remove()));
+ }
+ {
+ DecimalFormat df("#,##0", {"en", status}, status);
+ UnicodeString result;
+ assertEquals("pat #,##0: ", 3, df.getGroupingSize());
+ assertEquals("pat #,##0: ", (UBool) TRUE, (UBool) df.isGroupingUsed());
+ df.setGroupingUsed(false);
+ assertEquals("pat #,##0 then disabled: ", 3, df.getGroupingSize());
+ assertEquals("pat #,##0 then disabled: ", u"1111", df.format(1111, result.remove()));
+ df.setGroupingUsed(true);
+ assertEquals("pat #,##0 then enabled: ", 3, df.getGroupingSize());
+ assertEquals("pat #,##0 then enabled: ", u"1,111", df.format(1111, result.remove()));
+ }
+ // end bug 13442 comment 14
+
status = U_ZERO_ERROR;
const UnicodeString pattern("#,##0.# FF");
DecimalFormat pat(pattern, status);
private transient boolean exponentSignAlwaysShown;
private transient int formatWidth;
private transient int groupingSize;
+ private transient boolean groupingUsed;
private transient int magnitudeMultiplier;
private transient MathContext mathContext; // ICU4J-only
private transient int maximumFractionDigits;
exponentSignAlwaysShown = false;
formatWidth = -1;
groupingSize = -1;
+ groupingUsed = false;
magnitudeMultiplier = 0;
mathContext = null;
maximumFractionDigits = -1;
exponentSignAlwaysShown = other.exponentSignAlwaysShown;
formatWidth = other.formatWidth;
groupingSize = other.groupingSize;
+ groupingUsed = other.groupingUsed;
magnitudeMultiplier = other.magnitudeMultiplier;
mathContext = other.mathContext;
maximumFractionDigits = other.maximumFractionDigits;
eq = eq && _equalsHelper(exponentSignAlwaysShown, other.exponentSignAlwaysShown);
eq = eq && _equalsHelper(formatWidth, other.formatWidth);
eq = eq && _equalsHelper(groupingSize, other.groupingSize);
+ eq = eq && _equalsHelper(groupingUsed, other.groupingUsed);
eq = eq && _equalsHelper(magnitudeMultiplier, other.magnitudeMultiplier);
eq = eq && _equalsHelper(mathContext, other.mathContext);
eq = eq && _equalsHelper(maximumFractionDigits, other.maximumFractionDigits);
hashCode ^= _hashCodeHelper(exponentSignAlwaysShown);
hashCode ^= _hashCodeHelper(formatWidth);
hashCode ^= _hashCodeHelper(groupingSize);
+ hashCode ^= _hashCodeHelper(groupingUsed);
hashCode ^= _hashCodeHelper(magnitudeMultiplier);
hashCode ^= _hashCodeHelper(mathContext);
hashCode ^= _hashCodeHelper(maximumFractionDigits);
return groupingSize;
}
+ public boolean getGroupingUsed() {
+ return groupingUsed;
+ }
+
public int getMagnitudeMultiplier() {
return magnitudeMultiplier;
}
return this;
}
+ /**
+ * Sets whether to enable grouping when formatting.
+ *
+ * @param groupingUsed
+ * true to enable the display of grouping separators; false to disable.
+ * @return The property bag, for chaining.
+ */
+ public DecimalFormatProperties setGroupingUsed(boolean groupingUsed) {
+ this.groupingUsed = groupingUsed;
+ return this;
+ }
+
/**
* Multiply all numbers by this power of ten before formatting. Negative multipliers reduce the
* magnitude and make numbers smaller (closer to zero).
* Resolve the values in Properties to a Grouper object.
*/
public static Grouper forProperties(DecimalFormatProperties properties) {
+ if (!properties.getGroupingUsed()) {
+ return GROUPER_NEVER;
+ }
short grouping1 = (short) properties.getGroupingSize();
short grouping2 = (short) properties.getSecondaryGroupingSize();
short minGrouping = (short) properties.getMinimumGroupingDigits();
short grouping3 = (short) ((positive.groupingSizes >>> 32) & 0xffff);
if (grouping2 != -1) {
properties.setGroupingSize(grouping1);
+ properties.setGroupingUsed(true);
} else {
properties.setGroupingSize(-1);
+ properties.setGroupingUsed(false);
}
if (grouping3 != -1) {
properties.setSecondaryGroupingSize(grouping2);
*/
@Override
public synchronized boolean isGroupingUsed() {
- return properties.getGroupingSize() > 0 || properties.getSecondaryGroupingSize() > 0;
+ return properties.getGroupingUsed();
}
/**
*/
@Override
public synchronized void setGroupingUsed(boolean enabled) {
- if (enabled) {
- // Set to a reasonable default value
- properties.setGroupingSize(3);
- properties.setSecondaryGroupingSize(-1);
- } else {
- properties.setGroupingSize(0);
- properties.setSecondaryGroupingSize(0);
- }
+ properties.setGroupingUsed(enabled);
refreshFormatter();
}
* @stable ICU 2.0
*/
public synchronized int getGroupingSize() {
+ if (properties.getGroupingSize() < 0) {
+ return 0;
+ }
return properties.getGroupingSize();
}
" (unknown currency)",
nf.getPositiveSuffix());
}
+
+ @Test
+ public void TestGroupingEnabledDisabledGetters() {
+ // See ticket #13442 comment 14
+ DecimalFormatSymbols EN = DecimalFormatSymbols.getInstance(ULocale.ENGLISH);
+ {
+ DecimalFormat df = new DecimalFormat("0", EN);
+ assertEquals("pat 0: ", 0, df.getGroupingSize());
+ assertEquals("pat 0: ", false, df.isGroupingUsed());
+ df.setGroupingUsed(false);
+ assertEquals("pat 0 then disabled: ", 0, df.getGroupingSize());
+ assertEquals("pat 0 then disabled: ", "1111", df.format(1111));
+ df.setGroupingUsed(true);
+ assertEquals("pat 0 then enabled: ", 0, df.getGroupingSize());
+ assertEquals("pat 0 then enabled: ", "1111", df.format(1111));
+ }
+ {
+ DecimalFormat df = new DecimalFormat("#,##0", EN);
+ assertEquals("pat #,##0: ", 3, df.getGroupingSize());
+ assertEquals("pat #,##0: ", true, df.isGroupingUsed());
+ df.setGroupingUsed(false);
+ assertEquals("pat #,##0 then disabled: ", 3, df.getGroupingSize());
+ assertEquals("pat #,##0 then disabled: ", "1111", df.format(1111));
+ df.setGroupingUsed(true);
+ assertEquals("pat #,##0 then enabled: ", 3, df.getGroupingSize());
+ assertEquals("pat #,##0 then enabled: ", "1,111", df.format(1111));
+ }
+ }
}
// Grouping separators are not allowed in the pattern, but we can enable them via the API.
DecimalFormat df = new DecimalFormat("###0.000E0");
df.setGroupingUsed(true);
+ df.setGroupingSize(3);
expect2(df, 123, "123.0E0");
expect2(df, 1234, "1,234E0");
expect2(df, 12340, "1.234E4");
130, resultScientific.longValue());
}
- @Test
- public void Test13442() {
- DecimalFormat df = new DecimalFormat();
- df.setGroupingUsed(false);
- assertEquals("Grouping size should now be zero", 0, df.getGroupingSize());
- assertEquals("Grouping should be off", false, df.isGroupingUsed());
- }
-
@Test
public void Test13453_AffixContent() {
DecimalFormat df = (DecimalFormat) DecimalFormat.getScientificInstance();