Skip to content

Commit

Permalink
be: clearNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed May 22, 2024
1 parent c99c9e6 commit 9646d53
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>cn.devezhao</groupId>
<artifactId>persist4j</artifactId>
<version>1.7.9</version>
<version>1.7.10</version>
<packaging>jar</packaging>

<name>persist4j</name>
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/cn/devezhao/persist4j/record/RecordVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 9646d53

Please sign in to comment.