use CellFormat for all format strings containing multiple parts (";" delimited) and update unit test to expect the same values as Excel.  Also added tests for the failing formats.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1791949 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Greg Woolsey 2017-04-19 18:10:52 +00:00
parent d4d08d5ce8
commit d1b92fe2d8
2 changed files with 14 additions and 5 deletions

View File

@ -312,14 +312,13 @@ public class DataFormatter implements Observer {
String formatStr = formatStrIn; String formatStr = formatStrIn;
// Excel supports 3+ part conditional data formats, eg positive/negative/zero, // Excel supports 2+ part conditional data formats, eg positive/negative/zero,
// or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds // or (>1000),(>0),(0),(negative). As Java doesn't handle these kinds
// of different formats for different ranges, just +ve/-ve, we need to // of different formats for different ranges, just +ve/-ve, we need to
// handle these ourselves in a special way. // handle these ourselves in a special way.
// For now, if we detect 3+ parts, we call out to CellFormat to handle it // For now, if we detect 2+ parts, we call out to CellFormat to handle it
// TODO Going forward, we should really merge the logic between the two classes // TODO Going forward, we should really merge the logic between the two classes
if (formatStr.contains(";") && if (formatStr.contains(";") ) {
formatStr.indexOf(';') != formatStr.lastIndexOf(';')) {
try { try {
// Ask CellFormat to get a formatter for it // Ask CellFormat to get a formatter for it
CellFormat cfmt = CellFormat.getInstance(formatStr); CellFormat cfmt = CellFormat.getInstance(formatStr);

View File

@ -199,6 +199,15 @@ public class TestDataFormatter {
} }
} }
@Test
public void testConditionalRanges() {
DataFormatter dfUS = new DataFormatter(Locale.US);
String format = "[>=10]#,##0;[<10]0.0";
assertEquals("Wrong format for " + format, "17,876", dfUS.formatRawCellContents(17876.000, -1, format));
assertEquals("Wrong format for " + format, "9.7", dfUS.formatRawCellContents(9.71, -1, format));
}
/** /**
* Test how we handle negative and zeros. * Test how we handle negative and zeros.
* Note - some tests are disabled as DecimalFormat * Note - some tests are disabled as DecimalFormat
@ -309,7 +318,8 @@ public class TestDataFormatter {
//assertEquals("123", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0")); //assertEquals("123", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0"));
//Bug54868 patch has a hit on the first string before the ";" //Bug54868 patch has a hit on the first string before the ";"
assertEquals("-123 1/3", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0")); assertEquals("123", dfUS.formatRawCellContents(-123.321, -1, "0 ?/?;0"));
assertEquals("123 1/3", dfUS.formatRawCellContents(123.321, -1, "0 ?/?;0"));
//Bug53150 formatting a whole number with fractions should just give the number //Bug53150 formatting a whole number with fractions should just give the number
assertEquals("1", dfUS.formatRawCellContents(1.0, -1, "# #/#")); assertEquals("1", dfUS.formatRawCellContents(1.0, -1, "# #/#"));