From 74be4d4174109ff316ed2c7a66b86472152fffc4 Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Tue, 7 Jun 2016 06:18:44 +0000 Subject: [PATCH] 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 --- .../poi/ss/usermodel/DataFormatter.java | 17 +++++++++++------ .../poi/ss/usermodel/TestDataFormatter.java | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 6f0d830d3..a20d08328 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -674,13 +674,18 @@ public class DataFormatter implements Observer { // eg for a format like #'##0 which wants 12'345 not 12,345 Matcher agm = alternateGrouping.matcher(format); if (agm.find()) { - symbols = DecimalFormatSymbols.getInstance(locale); - char grouping = agm.group(2).charAt(0); - symbols.setGroupingSeparator(grouping); - String oldPart = agm.group(1); - String newPart = oldPart.replace(grouping, ','); - format = format.replace(oldPart, newPart); + // 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.setGroupingSeparator(grouping); + String oldPart = agm.group(1); + String newPart = oldPart.replace(grouping, ','); + format = format.replace(oldPart, newPart); + } } try { diff --git a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java index bc9ebed80..d827355cb 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java +++ b/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java @@ -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 * zapped from within the format strings