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
This commit is contained in:
Nick Burch 2010-06-03 16:20:26 +00:00
parent da0c5ad895
commit ee937dd9d0
3 changed files with 30 additions and 3 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.7-SNAPSHOT" date="2010-??-??"> <release version="3.7-SNAPSHOT" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="fix">49377 - only call DecimalFormat.setRoundingMode on Java 1.6 - it's needed to match excel's rendering of numbers</action>
<action dev="POI-DEVELOPERS" type="fix">49378 - correct 1.6ism</action> <action dev="POI-DEVELOPERS" type="fix">49378 - correct 1.6ism</action>
<action dev="POI-DEVELOPERS" type="add">Parse the HSMF headers chunk if present, and use it to find Dates in text extraction if needed</action> <action dev="POI-DEVELOPERS" type="add">Parse the HSMF headers chunk if present, and use it to find Dates in text extraction if needed</action>
<action dev="POI-DEVELOPERS" type="fix">48494 - detect and support time formats like HH:MM;HH:MM</action> <action dev="POI-DEVELOPERS" type="fix">48494 - detect and support time formats like HH:MM;HH:MM</action>

View File

@ -19,6 +19,8 @@ package org.apache.poi.ss.usermodel;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.*; import java.util.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.text.*; import java.text.*;
@ -415,7 +417,7 @@ public class DataFormatter {
try { try {
DecimalFormat df = new DecimalFormat(sb.toString(), decimalSymbols); DecimalFormat df = new DecimalFormat(sb.toString(), decimalSymbols);
df.setRoundingMode(RoundingMode.HALF_UP); setExcelStyleRoundingMode(df);
return df; return df;
} catch(IllegalArgumentException iae) { } catch(IllegalArgumentException iae) {
@ -659,6 +661,30 @@ public class DataFormatter {
result.setParseIntegerOnly(true); result.setParseIntegerOnly(true);
return result; 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 * Format class for Excel's SSN format. This class mimics Excel's built-in
* SSN formatting. * SSN formatting.

View File

@ -43,8 +43,8 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat {
private DecimalFormat format1digit = new DecimalFormat("0"); private DecimalFormat format1digit = new DecimalFormat("0");
private DecimalFormat format2digits = new DecimalFormat("00"); private DecimalFormat format2digits = new DecimalFormat("00");
{ {
format1digit.setRoundingMode(RoundingMode.HALF_UP); DataFormatter.setExcelStyleRoundingMode(format1digit);
format2digits.setRoundingMode(RoundingMode.HALF_UP); DataFormatter.setExcelStyleRoundingMode(format2digits);
} }
private double dateToBeFormatted = 0.0; private double dateToBeFormatted = 0.0;