From 091e464ed4a5b82240f03848a54ec0346bca4727 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 28 Dec 2011 05:10:24 +0000 Subject: [PATCH] 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 --- src/documentation/content/xdocs/status.xml | 1 + .../org/apache/poi/ss/usermodel/DataFormatter.java | 12 ++++++++++-- .../apache/poi/ss/usermodel/TestDataFormatter.java | 10 ++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 3a3d523b9..5c420b358 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 52389 - Support ?/? as well as #/# fractions, and tighten DataFormatter rules for fraction matching 52200 - Updated XWPF table example code 52378 - Support for WORKDAY and NETWORKDAYS functions 52349 - Merge the logic between the TEXT function and DataFormatter diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 9c56adaa4..c041e3237 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -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