aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2015-10-06 22:52:05 +0000
committerAndreas Beeker <kiwiwings@apache.org>2015-10-06 22:52:05 +0000
commit15c4dc67fe348877cc2ebf608d14b4f4824aabbb (patch)
treef16b2ffd97e3ac23896e133c970fbe2ac941a5ce
parent52564877e3dcc22b6b1cb5041a6c1dada258e6ab (diff)
downloadpoi-15c4dc67fe348877cc2ebf608d14b4f4824aabbb.tar.gz
poi-15c4dc67fe348877cc2ebf608d14b4f4824aabbb.zip
fix forbidden apis issue and make non-static as decimalformat is not thread-safe
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1707166 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java34
1 files changed, 18 insertions, 16 deletions
diff --git a/src/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java b/src/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
index 2f5ec79be1..8b90d8589d 100644
--- a/src/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
+++ b/src/java/org/apache/poi/ss/usermodel/ExcelGeneralNumberFormat.java
@@ -30,27 +30,29 @@ import java.text.Format;
import java.text.ParsePosition;
import java.util.Locale;
-/** A format that formats a double as Excel would, ignoring FieldPosition. All other operations are unsupported. */
+/**
+ * A format that formats a double as Excel would, ignoring FieldPosition.
+ * All other operations are unsupported.
+ **/
public class ExcelGeneralNumberFormat extends Format {
private static final long serialVersionUID = 1L;
- private static final DecimalFormat SCIENTIFIC_FORMAT = new DecimalFormat("0.#####E0");
- static {
- SCIENTIFIC_FORMAT.setRoundingMode(RoundingMode.HALF_UP);
- }
private static final MathContext TO_10_SF = new MathContext(10, RoundingMode.HALF_UP);
private final DecimalFormatSymbols decimalSymbols;
- private final DecimalFormat wholeNumFormat;
- private final DecimalFormat decimalNumFormat;
+ private final DecimalFormat integerFormat;
+ private final DecimalFormat decimalFormat;
+ private final DecimalFormat scientificFormat;
public ExcelGeneralNumberFormat(final Locale locale) {
decimalSymbols = new DecimalFormatSymbols(locale);
- wholeNumFormat = new DecimalFormat("#", decimalSymbols);
- DataFormatter.setExcelStyleRoundingMode(wholeNumFormat);
- decimalNumFormat = new DecimalFormat("#.##########", decimalSymbols);
- DataFormatter.setExcelStyleRoundingMode(decimalNumFormat);
+ scientificFormat = new DecimalFormat("0.#####E0", decimalSymbols);
+ DataFormatter.setExcelStyleRoundingMode(scientificFormat);
+ integerFormat = new DecimalFormat("#", decimalSymbols);
+ DataFormatter.setExcelStyleRoundingMode(integerFormat);
+ decimalFormat = new DecimalFormat("#.##########", decimalSymbols);
+ DataFormatter.setExcelStyleRoundingMode(decimalFormat);
}
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
@@ -58,26 +60,26 @@ public class ExcelGeneralNumberFormat extends Format {
if (number instanceof Number) {
value = ((Number)number).doubleValue();
if (Double.isInfinite(value) || Double.isNaN(value)) {
- return wholeNumFormat.format(number, toAppendTo, pos);
+ return integerFormat.format(number, toAppendTo, pos);
}
} else {
// testBug54786 gets here with a date, so retain previous behaviour
- return wholeNumFormat.format(number, toAppendTo, pos);
+ return integerFormat.format(number, toAppendTo, pos);
}
final double abs = Math.abs(value);
if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
- return SCIENTIFIC_FORMAT.format(number, toAppendTo, pos);
+ return scientificFormat.format(number, toAppendTo, pos);
} else if (Math.floor(value) == value || abs >= 1E10) {
// integer, or integer portion uses all 11 allowed digits
- return wholeNumFormat.format(number, toAppendTo, pos);
+ return integerFormat.format(number, toAppendTo, pos);
}
// Non-integers of non-scientific magnitude are formatted as "up to 11
// numeric characters, with the decimal point counting as a numeric
// character". We know there is a decimal point, so limit to 10 digits.
// https://support.microsoft.com/en-us/kb/65903
final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
- return decimalNumFormat.format(rounded, toAppendTo, pos);
+ return decimalFormat.format(rounded, toAppendTo, pos);
}
public Object parseObject(String source, ParsePosition pos) {