Skip to content

Commit

Permalink
fix formatting for Cell.NUMERIC when integer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Duda committed Dec 20, 2020
1 parent 67f3171 commit b237227
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/main/java/com/codingchili/excelastic/model/DataTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ public static Object parseString(String value) {
return value;
}
}

/**
* @param numericCellValue parameter to convert to either double or int.
* @return an integer or float object, depending on if the given param has decimal value > 0.
*/
public static Object parseNumeric(Double numericCellValue) {
if (numericCellValue - numericCellValue.intValue() > 0.0f) {
return numericCellValue;
} else {
return numericCellValue.intValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private JsonObject getRow(String[] titles, Row row, boolean dryRun) {
if (DateUtil.isCellDateFormatted(cell)) {
value = cell.getDateCellValue().toInstant().toString();
} else {
value = cell.getNumericCellValue();
value = DataTypes.parseNumeric(cell.getNumericCellValue());
}
break;
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/codingchili/TestDataType.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ public void parseString() {
Assert.assertEquals(TestDataType.<String>bytes("meow"), "meow");
}

@Test
public void numericAsFloat() {
Assert.assertEquals(DataTypes.parseNumeric(3.14), 3.14d);
}

@Test
public void numericAsInt() {
Assert.assertEquals(DataTypes.parseNumeric(3.0), 3);
}

@SuppressWarnings("unchecked")
private static <T> T bytes(String string) {
return (T) DataTypes.parseBytes(string.getBytes(StandardCharsets.UTF_8));
Expand Down

0 comments on commit b237227

Please sign in to comment.