diff --git a/pom.xml b/pom.xml index 47748ad..9d4f66a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ cn.devezhao persist4j - 1.7.9 + 1.7.10 jar persist4j diff --git a/src/main/java/cn/devezhao/persist4j/record/RecordVisitor.java b/src/main/java/cn/devezhao/persist4j/record/RecordVisitor.java index 6361bc2..5a0c7dc 100644 --- a/src/main/java/cn/devezhao/persist4j/record/RecordVisitor.java +++ b/src/main/java/cn/devezhao/persist4j/record/RecordVisitor.java @@ -72,16 +72,16 @@ public static void setValueByLiteral(Field field, String value, Record record) { } else if (FieldType.INT.equals(ft) || FieldType.SMALL_INT.equals(ft)) { - pVal = NumberUtils.toInt(clearNumber(value)); + pVal = NumberUtils.toInt(clearNumber(value, Boolean.TRUE)); } else if (FieldType.DOUBLE.equals(ft)) { - pVal = NumberUtils.toDouble(clearNumber(value)); + pVal = NumberUtils.toDouble(clearNumber(value, Boolean.FALSE)); } else if (FieldType.DECIMAL.equals(ft)) { - pVal = new BigDecimal(clearNumber(value).toCharArray()); + pVal = new BigDecimal(clearNumber(value, Boolean.FALSE).toCharArray()); } else if (FieldType.LONG.equals(ft)) { - pVal = NumberUtils.toLong(clearNumber(value)); + pVal = NumberUtils.toLong(clearNumber(value, Boolean.TRUE)); } else if (FieldType.DATE.equals(ft) || FieldType.TIMESTAMP.equals(ft)) { @@ -162,12 +162,16 @@ public static String getLiteralByValue(Field field, Object value) { /** * @param num + * @param clearDecimal * @return */ - protected static String clearNumber(String num) { + protected static String clearNumber(String num, boolean clearDecimal) { if (StringUtils.isBlank(num)) return "0"; num = num.replace(",", "").trim(); - if (NumberUtils.isNumber(num)) return num; + if (NumberUtils.isNumber(num)) { + if (clearDecimal) return num.split("\\.")[0]; + return num; + } LOG.warn("Bad number format : " + num); return "0"; diff --git a/src/test/java/cn/devezhao/persist4j/record/RecordVisitorTest.java b/src/test/java/cn/devezhao/persist4j/record/RecordVisitorTest.java index 80a338e..84cff42 100644 --- a/src/test/java/cn/devezhao/persist4j/record/RecordVisitorTest.java +++ b/src/test/java/cn/devezhao/persist4j/record/RecordVisitorTest.java @@ -6,10 +6,10 @@ public class RecordVisitorTest { @Test public void testClearNumber() { - System.out.println(RecordVisitor.clearNumber("123 ")); - System.out.println(RecordVisitor.clearNumber("123d ")); - System.out.println(RecordVisitor.clearNumber("1,123 ")); - System.out.println(RecordVisitor.clearNumber("111,1,11,,123.00 ")); - System.out.println(RecordVisitor.clearNumber("NaN")); + System.out.println(RecordVisitor.clearNumber("123 ", Boolean.TRUE)); + System.out.println(RecordVisitor.clearNumber("123d ", Boolean.TRUE)); + System.out.println(RecordVisitor.clearNumber("1,123 ", Boolean.TRUE)); + System.out.println(RecordVisitor.clearNumber("111,1,11,,123.00 ", Boolean.FALSE)); + System.out.println(RecordVisitor.clearNumber("NaN", Boolean.TRUE)); } } \ No newline at end of file