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
This commit is contained in:
parent
20ba4851a8
commit
0e7caa5eb1
@ -157,6 +157,12 @@ public class DataFormatter implements Observer {
|
|||||||
*/
|
*/
|
||||||
private static final Pattern fractionStripper = Pattern.compile("(\"[^\"]*\")|([^ \\?#\\d\\/]+)");
|
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
|
* Cells formatted with a date or time format and which contain invalid date or time values
|
||||||
* show 255 pound signs ("#").
|
* show 255 pound signs ("#").
|
||||||
@ -658,10 +664,24 @@ public class DataFormatter implements Observer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Format createNumberFormat(String formatStr, double cellValue) {
|
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 {
|
try {
|
||||||
DecimalFormat df = new DecimalFormat(format, decimalSymbols);
|
DecimalFormat df = new DecimalFormat(format, symbols);
|
||||||
setExcelStyleRoundingMode(df);
|
setExcelStyleRoundingMode(df);
|
||||||
return df;
|
return df;
|
||||||
} catch(IllegalArgumentException iae) {
|
} catch(IllegalArgumentException iae) {
|
||||||
|
@ -165,4 +165,40 @@ public abstract class BaseTestDataFormat extends TestCase {
|
|||||||
// TODO Fix this to not have an extra 0 at the end
|
// TODO Fix this to not have an extra 0 at the end
|
||||||
//assertEquals(pound+" - ", formatter.formatCellValue(zero));
|
//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…
Reference in New Issue
Block a user