Browse Source

Update NumberUtils.java

数字转换时,去掉前后空格,提升Excel导入时兼容性,避免因多输入空格导致无法转换的问题
pull/2961/head
China.Roger 2 years ago committed by GitHub
parent
commit
e9cdeb4194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      easyexcel-core/src/main/java/com/alibaba/excel/util/NumberUtils.java

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

@ -75,9 +75,9 @@ public class NumberUtils {
*/
public static Short parseShort(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).shortValue();
return new BigDecimal(string.trim()).shortValue();
}
return parse(string, contentProperty).shortValue();
return parse(string.trim(), contentProperty).shortValue();
}
/**
@ -89,9 +89,9 @@ public class NumberUtils {
*/
public static Long parseLong(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).longValue();
return new BigDecimal(string.trim()).longValue();
}
return parse(string, contentProperty).longValue();
return parse(string.trim(), contentProperty).longValue();
}
/**
@ -103,9 +103,9 @@ public class NumberUtils {
*/
public static Integer parseInteger(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).intValue();
return new BigDecimal(string.trim()).intValue();
}
return parse(string, contentProperty).intValue();
return parse(string.trim(), contentProperty).intValue();
}
/**
@ -117,9 +117,9 @@ public class NumberUtils {
*/
public static Float parseFloat(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).floatValue();
return new BigDecimal(string.trim()).floatValue();
}
return parse(string, contentProperty).floatValue();
return parse(string.trim(), contentProperty).floatValue();
}
/**
@ -132,9 +132,9 @@ public class NumberUtils {
public static BigDecimal parseBigDecimal(String string, ExcelContentProperty contentProperty)
throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string);
return new BigDecimal(string.trim());
}
return new BigDecimal(parse(string, contentProperty).toString());
return new BigDecimal(parse(string.trim(), contentProperty).toString());
}
/**
@ -146,9 +146,9 @@ public class NumberUtils {
*/
public static Byte parseByte(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).byteValue();
return new BigDecimal(string.trim()).byteValue();
}
return parse(string, contentProperty).byteValue();
return parse(string.trim(), contentProperty).byteValue();
}
/**
@ -160,9 +160,9 @@ public class NumberUtils {
*/
public static Double parseDouble(String string, ExcelContentProperty contentProperty) throws ParseException {
if (!hasFormat(contentProperty)) {
return new BigDecimal(string).doubleValue();
return new BigDecimal(string.trim()).doubleValue();
}
return parse(string, contentProperty).doubleValue();
return parse(string.trim(), contentProperty).doubleValue();
}
private static boolean hasFormat(ExcelContentProperty contentProperty) {

Loading…
Cancel
Save