Browse Source

Bug #55265 - DataFormatter correct support for alternate number grouping characters, eg 1234 + #'##0 = 1'234 not 1,234

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1712605 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_14_BETA1
Nick Burch 8 years ago
parent
commit
d1ea6125fa

+ 22
- 2
src/java/org/apache/poi/ss/usermodel/DataFormatter.java View File

@@ -157,6 +157,12 @@ public class DataFormatter implements Observer {
*/
private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ \\?#\\d\\/]+)");

/**
* A regex to detect if an alternate grouping character is used
* in a numeric format
*/
private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})");
/**
* Cells formatted with a date or time format and which contain invalid date or time values
* show 255 pound signs ("#").
@@ -658,10 +664,24 @@ public class DataFormatter implements Observer {
}

private Format createNumberFormat(String formatStr, double cellValue) {
final String format = cleanFormatForNumber(formatStr);
String format = cleanFormatForNumber(formatStr);
DecimalFormatSymbols symbols = decimalSymbols;
// Do we need to change the grouping character?
// 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);
}
try {
DecimalFormat df = new DecimalFormat(format, decimalSymbols);
DecimalFormat df = new DecimalFormat(format, symbols);
setExcelStyleRoundingMode(df);
return df;
} catch(IllegalArgumentException iae) {

+ 36
- 0
src/testcases/org/apache/poi/ss/usermodel/BaseTestDataFormat.java View File

@@ -165,4 +165,40 @@ public abstract class BaseTestDataFormat extends TestCase {
// TODO Fix this to not have an extra 0 at the end
//assertEquals(pound+" - ", formatter.formatCellValue(zero));
}
/**
* Using a single quote (') instead of a comma (,) as
* a number separator, eg 1000 -> 1'000
*/
public final void test55265() {
Workbook wb = _testDataProvider.createWorkbook();
DataFormatter formatter = new DataFormatter();
DataFormat fmt = wb.createDataFormat();
Sheet sheet = wb.createSheet();
Row r = sheet.createRow(0);
CellStyle cs = wb.createCellStyle();
cs.setDataFormat(fmt.getFormat("#'##0"));
Cell zero = r.createCell(0);
zero.setCellValue(0);
zero.setCellStyle(cs);
Cell sml = r.createCell(1);
sml.setCellValue(12);
sml.setCellStyle(cs);
Cell med = r.createCell(2);
med.setCellValue(1234);
med.setCellStyle(cs);
Cell lge = r.createCell(3);
lge.setCellValue(12345678);
lge.setCellStyle(cs);
assertEquals("0", formatter.formatCellValue(zero));
assertEquals("12", formatter.formatCellValue(sml));
assertEquals("1'234", formatter.formatCellValue(med));
assertEquals("12'345'678", formatter.formatCellValue(lge));
}
}

Loading…
Cancel
Save