mirror of https://github.com/apache/poi.git
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:
parent
34760dac11
commit
05bfb7c0c9
|
@ -34,6 +34,7 @@
|
||||||
|
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.8-beta6" date="2012-??-??">
|
<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">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">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>
|
<action dev="poi-developers" type="add">52349 - Merge the logic between the TEXT function and DataFormatter</action>
|
||||||
|
|
|
@ -349,8 +349,9 @@ public class DataFormatter {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Excel supports fractions in format strings, which Java doesn't
|
// Excel supports fractions in format strings, which Java doesn't
|
||||||
if (formatStr.indexOf("/") == formatStr.lastIndexOf("/") &&
|
if (!formatStr.contains("-") &&
|
||||||
formatStr.indexOf("/") >= 0 && !formatStr.contains("-")) {
|
(formatStr.indexOf("#/#") >= 0 && formatStr.indexOf("#/#") == formatStr.lastIndexOf("#/#")) ||
|
||||||
|
(formatStr.indexOf("?/?") >= 0 && formatStr.indexOf("?/?") == formatStr.lastIndexOf("?/?"))) {
|
||||||
return new FractionFormat(formatStr);
|
return new FractionFormat(formatStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -985,6 +986,8 @@ public class DataFormatter {
|
||||||
if (wholePart * decPart == 0) {
|
if (wholePart * decPart == 0) {
|
||||||
return "0";
|
return "0";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Split the format string into decimal and fraction parts
|
||||||
String[] parts = str.split(" ");
|
String[] parts = str.split(" ");
|
||||||
String[] fractParts;
|
String[] fractParts;
|
||||||
if (parts.length == 2) {
|
if (parts.length == 2) {
|
||||||
|
@ -992,6 +995,11 @@ public class DataFormatter {
|
||||||
} else {
|
} else {
|
||||||
fractParts = str.split("/");
|
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) {
|
if (fractParts.length == 2) {
|
||||||
double minVal = 1.0;
|
double minVal = 1.0;
|
||||||
|
|
|
@ -168,9 +168,15 @@ public class TestDataFormatter extends TestCase {
|
||||||
public void testFractions() {
|
public void testFractions() {
|
||||||
DataFormatter dfUS = new DataFormatter(Locale.US);
|
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("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, "?/??"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue