Browse Source

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
tags/REL_3_7_BETA1
Nick Burch 14 years ago
parent
commit
2e26965241

+ 1
- 0
src/documentation/content/xdocs/status.xml View File

@@ -34,6 +34,7 @@

<changes>
<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="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>

+ 27
- 1
src/java/org/apache/poi/ss/usermodel/DataFormatter.java View File

@@ -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.

+ 2
- 2
src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java View File

@@ -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;

Loading…
Cancel
Save