Commit a77254d3287f885d8151057ca2fd8e249166fa0a
1 parent
2945681a
Exists in
master
fix a bug about format fraction to string
Showing
2 changed files
with
7 additions
and
63 deletions
Show diff stats
src/main/java/com/taover/easyexcel/metadata/format/POIFractionFormatOptimization.java
... | ... | @@ -20,7 +20,7 @@ import org.apache.poi.ss.formula.eval.NotImplementedException; |
20 | 20 | @SuppressWarnings("serial") |
21 | 21 | public class POIFractionFormatOptimization extends Format { |
22 | 22 | private final String wholePartFormatString; |
23 | - private final Format decimalFormatForIntStr = new DecimalFormat("#"); | |
23 | + private final Format decimalFormatForIntStr = new DecimalFormat("#.#"); | |
24 | 24 | |
25 | 25 | /** |
26 | 26 | * Single parameter ctor |
... | ... | @@ -32,74 +32,17 @@ public class POIFractionFormatOptimization extends Format { |
32 | 32 | |
33 | 33 | public String format(Number num) { |
34 | 34 | final double doubleValue = num.doubleValue(); |
35 | - | |
36 | - final boolean isNeg = (doubleValue < 0.0f) ? true : false; | |
37 | - final double absDoubleValue = Math.abs(doubleValue); | |
38 | - | |
39 | - final double wholePart = Math.floor(absDoubleValue); | |
40 | - final double decPart = absDoubleValue - wholePart; | |
41 | - if (wholePart + decPart == 0) { | |
42 | - return "0"; | |
43 | - } | |
44 | - | |
45 | - // if the absolute value is smaller than 1 over the exact or maxDenom | |
46 | - // you can stop here and return "0" | |
47 | - // reciprocal is result of an int devision ... and so it's nearly always 0 | |
48 | - // double reciprocal = 1/Math.max(exactDenom, maxDenom); | |
49 | - // if (absDoubleValue < reciprocal) { | |
50 | - // return "0"; | |
51 | - // } | |
52 | - | |
53 | - //this is necessary to prevent overflow in the maxDenom calculation | |
54 | - if (Double.compare(decPart, 0) == 0){ | |
55 | - StringBuilder sb = new StringBuilder(); | |
56 | - if (isNeg){ | |
57 | - sb.append("-"); | |
58 | - } | |
59 | - //sb.append((int)wholePart); //old code | |
60 | - sb.append(decimalFormatForIntStr.format(wholePart)); | |
61 | - return sb.toString(); | |
62 | - } | |
63 | - | |
64 | 35 | Fraction fract = null; |
65 | 36 | try{ |
66 | 37 | fract = Fraction.getFraction(doubleValue); |
67 | 38 | } catch (RuntimeException e){ |
68 | - return Double.toString(doubleValue); | |
39 | + return decimalFormatForIntStr.format(doubleValue); | |
69 | 40 | } |
70 | - | |
71 | - StringBuilder sb = new StringBuilder(); | |
72 | - | |
73 | - //now format the results | |
74 | - if (isNeg){ | |
75 | - sb.append("-"); | |
76 | - } | |
77 | - | |
78 | - //if whole part has to go into the numerator | |
79 | 41 | if ("".equals(wholePartFormatString)){ |
80 | - //int trueNum = (fract.getDenominator()*(int)wholePart)+fract.getNumerator(); //old code | |
81 | - //sb.append(trueNum).append("/").append(fract.getDenominator()); //old code | |
82 | - sb.append(decimalFormatForIntStr.format(fract.getDenominator()*wholePart+fract.getNumerator())).append("/").append(fract.getDenominator()); | |
83 | - return sb.toString(); | |
84 | - } | |
85 | - | |
86 | - //short circuit if fraction is 0 or 1 | |
87 | - if (fract.getNumerator() == 0){ | |
88 | - //sb.append(Integer.toString((int)wholePart)); //old code | |
89 | - sb.append(decimalFormatForIntStr.format(wholePart)); | |
90 | - return sb.toString(); | |
91 | - } else if (fract.getNumerator() == fract.getDenominator()){ | |
92 | - //sb.append(Integer.toString((int)wholePart+1)); //old code | |
93 | - sb.append(decimalFormatForIntStr.format(wholePart+1)); | |
94 | - return sb.toString(); | |
95 | - } | |
96 | - //as mentioned above, this ignores the exact space formatting in Excel | |
97 | - if (wholePart > 0){ | |
98 | - //sb.append(Integer.toString((int)wholePart)).append(" "); //old code | |
99 | - sb.append(decimalFormatForIntStr.format(wholePart)).append(" "); | |
42 | + return fract.toString(); | |
43 | + }else { | |
44 | + return fract.toProperString(); | |
100 | 45 | } |
101 | - sb.append(fract.getNumerator()).append("/").append(fract.getDenominator()); | |
102 | - return sb.toString(); | |
103 | 46 | } |
104 | 47 | |
105 | 48 | public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { | ... | ... |
src/test/java/com/taover/easyexcel/test/WbTest.java
... | ... | @@ -13,7 +13,8 @@ public class WbTest { |
13 | 13 | //File dataFile = new File("C:\\Users\\Administrator\\Desktop\\数据为空\\好合意商城_20210127_16_47(1).xlsx"); |
14 | 14 | //File dataFile = new File("C:\\Users\\Administrator\\Desktop\\Excel隐藏行\\配送确认1.16(1).xlsx"); |
15 | 15 | //File dataFile = new File("C:\\Users\\Administrator\\Desktop\\Excel隐藏行\\吉祥三宝-20210128-29条(1).xlsx"); |
16 | - File dataFile = new File("C:\\Users\\Administrator\\Desktop\\读入错误.xlsx"); | |
16 | + //File dataFile = new File("C:\\Users\\Administrator\\Desktop\\读入错误\\数值读入错误.xlsx"); | |
17 | + File dataFile = new File("C:\\Users\\Administrator\\Desktop\\读入错误\\数值读入错误-测试.xlsx"); | |
17 | 18 | //File dataFile = new File("C:\\Users\\Administrator\\Desktop\\表头匹配-测试文件\\多sheet-多个可用-数据有无.xlsx"); |
18 | 19 | List<List<Object>> data = transListMapTo2List(EasyExcel.read(dataFile).readHiddenRow(false).headRowNumber(0).doReadSelectedSync()); |
19 | 20 | for(List<Object> row: data) { | ... | ... |