Browse Source

* 提高类型转换兼容性 [Issue #2443](https://github.com/alibaba/easyexcel/issues/2443)

pull/2472/head
Jiaju Zhuang 3 years ago
parent
commit
346a1a8c56
  1. 27
      easyexcel-core/src/main/java/com/alibaba/excel/util/NumberUtils.java
  2. 1
      update.md

27
easyexcel-core/src/main/java/com/alibaba/excel/util/NumberUtils.java

@ -75,7 +75,7 @@ public class NumberUtils {
*/
public static Short parseShort(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return Short.valueOf(string);
return new BigDecimal(string).shortValue();
}
return parse(string, contentProperty).shortValue();
}
@ -89,7 +89,7 @@ public class NumberUtils {
*/
public static Long parseLong(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return Long.valueOf(string);
return new BigDecimal(string).longValue();
}
return parse(string, contentProperty).longValue();
}
@ -97,26 +97,13 @@ public class NumberUtils {
/**
* parse Integer from string
*
* @param string An integer read in string format
* @param string An integer read in string format
* @param contentProperty Properties of the content read in
* @return An integer converted from a string
*/
public static Integer parseInteger(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
// CS304 Issue link: https://github.com/alibaba/easyexcel/issues/2443
int stringLength = string.length();
if(stringLength>0){
int pointer = stringLength;
for(int i=0;i<stringLength;i++){
if(string.charAt(i)=='.'){
pointer = i;
break;
}
}
string = string.substring(0,pointer);
}
return Integer.valueOf(string);
return new BigDecimal(string).intValue();
}
return parse(string, contentProperty).intValue();
}
@ -130,7 +117,7 @@ public class NumberUtils {
*/
public static Float parseFloat(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return Float.valueOf(string);
return new BigDecimal(string).floatValue();
}
return parse(string, contentProperty).floatValue();
}
@ -159,7 +146,7 @@ public class NumberUtils {
*/
public static Byte parseByte(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return Byte.valueOf(string);
return new BigDecimal(string).byteValue();
}
return parse(string, contentProperty).byteValue();
}
@ -173,7 +160,7 @@ public class NumberUtils {
*/
public static Double parseDouble(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return Double.valueOf(string);
return new BigDecimal(string).doubleValue();
}
return parse(string, contentProperty).doubleValue();
}

1
update.md

@ -6,6 +6,7 @@
* 修复`filed`拼接错误 [Issue #2390](https://github.com/alibaba/easyexcel/issues/2390)
* 修复`BigIntegerStringConverter`无效的bug [Issue #2325](https://github.com/alibaba/easyexcel/issues/2325)
* 修复03版本无法读取到公式格式的bug [Issue #2444](https://github.com/alibaba/easyexcel/issues/2444)
* 提高类型转换兼容性 [Issue #2443](https://github.com/alibaba/easyexcel/issues/2443)
# 3.0.5

Loading…
Cancel
Save