Skip to content

Commit 324e01d

Browse files
committed
Preserve integer-exact representation of numbers and allow conversion of floating point values to integers
This differentiates floating point values that are within the range of exact integer correspondence and renders them as integers rather than using the default java toString method which formats floating point values in E-notation when the absolute value is at least 1e6.
1 parent 6bc1452 commit 324e01d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/ConvertProcessor.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ public Object convert(Object value) {
102102
STRING {
103103
@Override
104104
public Object convert(Object value) {
105+
if (isExactIntegerDouble(value)) {
106+
Long l = new Long((long) ((Double) value).doubleValue());
107+
return l.toString();
108+
}
109+
if (isExactIntegerFloat(value)) {
110+
Long l = new Long((long) ((Float) value).floatValue());
111+
return l.toString();
112+
}
105113
return value.toString();
106114
}
107115
},
@@ -149,6 +157,24 @@ public static Type fromString(String processorTag, String propertyName, String t
149157
);
150158
}
151159
}
160+
161+
private static boolean isExactIntegerFloat(Object value) {
162+
final float ABS_MAX_EXACT_FLOAT = (float) 0x1p24 - 1;
163+
if (!(value instanceof Float)) {
164+
return false;
165+
}
166+
float v = ((Float) value).floatValue();
167+
return v == (long) v && -ABS_MAX_EXACT_FLOAT <= v && v <= ABS_MAX_EXACT_FLOAT;
168+
}
169+
170+
private static boolean isExactIntegerDouble(Object value) {
171+
final double ABS_MAX_EXACT_DOUBLE = 0x1p53 - 1;
172+
if (!(value instanceof Double)) {
173+
return false;
174+
}
175+
double v = ((Double) value).doubleValue();
176+
return v == (long) v && -ABS_MAX_EXACT_DOUBLE <= v && v <= ABS_MAX_EXACT_DOUBLE;
177+
}
152178
}
153179

154180
public static final String TYPE = "convert";

0 commit comments

Comments
 (0)