From 2e269652413250a51dca74bb83213494b3e54b33 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 3 Jun 2010 16:20:26 +0000 Subject: [PATCH] Fix bug #49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@951052 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../poi/ss/usermodel/DataFormatter.java | 28 ++++++++++++++++++- .../ss/usermodel/ExcelStyleDateFormatter.java | 4 +-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index ccf3fff899..4ec3ca5ba3 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers 49378 - correct 1.6ism Parse the HSMF headers chunk if present, and use it to find Dates in text extraction if needed 48494 - detect and support time formats like HH:MM;HH:MM diff --git a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java index 69e1f2ae0d..36ba5a1b6b 100644 --- a/src/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -19,6 +19,8 @@ package org.apache.poi.ss.usermodel; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.util.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.math.RoundingMode; import java.text.*; @@ -415,7 +417,7 @@ public class DataFormatter { try { DecimalFormat df = new DecimalFormat(sb.toString(), decimalSymbols); - df.setRoundingMode(RoundingMode.HALF_UP); + setExcelStyleRoundingMode(df); return df; } catch(IllegalArgumentException iae) { @@ -659,6 +661,30 @@ public class DataFormatter { result.setParseIntegerOnly(true); return result; } + + /** + * Enables excel style rounding mode (round half up) + * on the Decimal Format if possible. + * This will work for Java 1.6, but isn't possible + * on Java 1.5. + */ + public static void setExcelStyleRoundingMode(DecimalFormat format) { + try { + Method srm = format.getClass().getMethod("setRoundingMode", RoundingMode.class); + srm.invoke(format, RoundingMode.HALF_UP); + } catch(NoSuchMethodException e) { + // Java 1.5 + } catch(IllegalAccessException iae) { + // Shouldn't happen + throw new RuntimeException("Unable to set rounding mode", iae); + } catch(InvocationTargetException ite) { + // Shouldn't happen + throw new RuntimeException("Unable to set rounding mode", ite); + } catch(SecurityException se) { + // Not much we can do here + } + } + /** * Format class for Excel's SSN format. This class mimics Excel's built-in * SSN formatting. diff --git a/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java b/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java index ebe67a734a..91ea083b56 100644 --- a/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java @@ -43,8 +43,8 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { private DecimalFormat format1digit = new DecimalFormat("0"); private DecimalFormat format2digits = new DecimalFormat("00"); { - format1digit.setRoundingMode(RoundingMode.HALF_UP); - format2digits.setRoundingMode(RoundingMode.HALF_UP); + DataFormatter.setExcelStyleRoundingMode(format1digit); + DataFormatter.setExcelStyleRoundingMode(format2digits); } private double dateToBeFormatted = 0.0; -- 2.39.5