Bug #52389 - Handle ?/? format fractions as well as #/# ones, and tighten the criteria for triggering fraction formatting matching

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1225093 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-12-28 05:10:24 +00:00
parent 34760dac11
commit 05bfb7c0c9
3 changed files with 19 additions and 4 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.8-beta6" date="2012-??-??">
<action dev="poi-developers" type="add">52389 - Support ?/? as well as #/# fractions, and tighten DataFormatter rules for fraction matching</action>
<action dev="poi-developers" type="add">52200 - Updated XWPF table example code </action>
<action dev="poi-developers" type="add">52378 - Support for WORKDAY and NETWORKDAYS functions</action>
<action dev="poi-developers" type="add">52349 - Merge the logic between the TEXT function and DataFormatter</action>

View File

@ -349,8 +349,9 @@ public class DataFormatter {
}
// Excel supports fractions in format strings, which Java doesn't
if (formatStr.indexOf("/") == formatStr.lastIndexOf("/") &&
formatStr.indexOf("/") >= 0 && !formatStr.contains("-")) {
if (!formatStr.contains("-") &&
(formatStr.indexOf("#/#") >= 0 && formatStr.indexOf("#/#") == formatStr.lastIndexOf("#/#")) ||
(formatStr.indexOf("?/?") >= 0 && formatStr.indexOf("?/?") == formatStr.lastIndexOf("?/?"))) {
return new FractionFormat(formatStr);
}
@ -985,6 +986,8 @@ public class DataFormatter {
if (wholePart * decPart == 0) {
return "0";
}
// Split the format string into decimal and fraction parts
String[] parts = str.split(" ");
String[] fractParts;
if (parts.length == 2) {
@ -992,6 +995,11 @@ public class DataFormatter {
} else {
fractParts = str.split("/");
}
// Excel supports both #/# and ?/?, but Java only the former
for (int i=0; i<fractParts.length; i++) {
fractParts[i] = fractParts[i].replace('?', '#');
}
if (fractParts.length == 2) {
double minVal = 1.0;

View File

@ -168,9 +168,15 @@ public class TestDataFormatter extends TestCase {
public void testFractions() {
DataFormatter dfUS = new DataFormatter(Locale.US);
assertEquals("321 1/3", dfUS.formatRawCellContents(321.321, -1, "# #/#"));
// Excel often prefers "# #/#"
assertEquals("321 1/3", dfUS.formatRawCellContents(321.321, -1, "# #/#"));
assertEquals("321 26/81", dfUS.formatRawCellContents(321.321, -1, "# #/##"));
assertEquals("26027/81", dfUS.formatRawCellContents(321.321, -1, "#/##"));
assertEquals("26027/81", dfUS.formatRawCellContents(321.321, -1, "#/##"));
// OOo seems to like the "# ?/?" form
assertEquals("321 1/3", dfUS.formatRawCellContents(321.321, -1, "# ?/?"));
assertEquals("321 26/81", dfUS.formatRawCellContents(321.321, -1, "# ?/??"));
assertEquals("26027/81", dfUS.formatRawCellContents(321.321, -1, "?/??"));
}
/**