Skip to content

Commit b237227

Browse files
author
Robin Duda
committed
fix formatting for Cell.NUMERIC when integer.
1 parent 67f3171 commit b237227

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/main/java/com/codingchili/excelastic/model/DataTypes.java

+12
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,16 @@ public static Object parseString(String value) {
4343
return value;
4444
}
4545
}
46+
47+
/**
48+
* @param numericCellValue parameter to convert to either double or int.
49+
* @return an integer or float object, depending on if the given param has decimal value > 0.
50+
*/
51+
public static Object parseNumeric(Double numericCellValue) {
52+
if (numericCellValue - numericCellValue.intValue() > 0.0f) {
53+
return numericCellValue;
54+
} else {
55+
return numericCellValue.intValue();
56+
}
57+
}
4658
}

src/main/java/com/codingchili/excelastic/model/ExcelParser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private JsonObject getRow(String[] titles, Row row, boolean dryRun) {
244244
if (DateUtil.isCellDateFormatted(cell)) {
245245
value = cell.getDateCellValue().toInstant().toString();
246246
} else {
247-
value = cell.getNumericCellValue();
247+
value = DataTypes.parseNumeric(cell.getNumericCellValue());
248248
}
249249
break;
250250
}

src/test/java/com/codingchili/TestDataType.java

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ public void parseString() {
3232
Assert.assertEquals(TestDataType.<String>bytes("meow"), "meow");
3333
}
3434

35+
@Test
36+
public void numericAsFloat() {
37+
Assert.assertEquals(DataTypes.parseNumeric(3.14), 3.14d);
38+
}
39+
40+
@Test
41+
public void numericAsInt() {
42+
Assert.assertEquals(DataTypes.parseNumeric(3.0), 3);
43+
}
44+
3545
@SuppressWarnings("unchecked")
3646
private static <T> T bytes(String string) {
3747
return (T) DataTypes.parseBytes(string.getBytes(StandardCharsets.UTF_8));

0 commit comments

Comments
 (0)