bug 59638: patch from Axel Howind, support non-comma number grouping separators (such as German 1.234,57)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1747139 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2016-06-07 06:18:44 +00:00
parent a749f7f3f5
commit 74be4d4174
2 changed files with 30 additions and 6 deletions

View File

@ -674,14 +674,19 @@ public class DataFormatter implements Observer {
// eg for a format like #'##0 which wants 12'345 not 12,345 // eg for a format like #'##0 which wants 12'345 not 12,345
Matcher agm = alternateGrouping.matcher(format); Matcher agm = alternateGrouping.matcher(format);
if (agm.find()) { if (agm.find()) {
char grouping = agm.group(2).charAt(0);
// Only replace the grouping character if it is not the default
// grouping character for the US locale (',') in order to enable
// correct grouping for non-US locales.
if (grouping!=',') {
symbols = DecimalFormatSymbols.getInstance(locale); symbols = DecimalFormatSymbols.getInstance(locale);
char grouping = agm.group(2).charAt(0);
symbols.setGroupingSeparator(grouping); symbols.setGroupingSeparator(grouping);
String oldPart = agm.group(1); String oldPart = agm.group(1);
String newPart = oldPart.replace(grouping, ','); String newPart = oldPart.replace(grouping, ',');
format = format.replace(oldPart, newPart); format = format.replace(oldPart, newPart);
} }
}
try { try {
DecimalFormat df = new DecimalFormat(format, symbols); DecimalFormat df = new DecimalFormat(format, symbols);

View File

@ -82,6 +82,25 @@ public class TestDataFormatter {
} }
/**
* Test that we use the specified locale when deciding
* how to format normal numbers
*/
@Test
public void testGrouping() {
DataFormatter dfUS = new DataFormatter(Locale.US);
DataFormatter dfDE = new DataFormatter(Locale.GERMAN);
assertEquals("1,234.57", dfUS.formatRawCellContents(1234.567, -1, "#,##0.00"));
assertEquals("1'234.57", dfUS.formatRawCellContents(1234.567, -1, "#'##0.00"));
assertEquals("1 234.57", dfUS.formatRawCellContents(1234.567, -1, "# ##0.00"));
assertEquals("1.234,57", dfDE.formatRawCellContents(1234.567, -1, "#,##0.00"));
assertEquals("1'234,57", dfDE.formatRawCellContents(1234.567, -1, "#'##0.00"));
assertEquals("1 234,57", dfDE.formatRawCellContents(1234.567, -1, "# ##0.00"));
}
/** /**
* Ensure that colours get correctly * Ensure that colours get correctly
* zapped from within the format strings * zapped from within the format strings