<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>
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.*;
try {
DecimalFormat df = new DecimalFormat(sb.toString(), decimalSymbols);
- df.setRoundingMode(RoundingMode.HALF_UP);
+ setExcelStyleRoundingMode(df);
return df;
} catch(IllegalArgumentException iae) {
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.
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;