# Elasticsearch stats aggregation corrupt float number precision

**URL:** <https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926>\
**Category:** Elasticsearch\
**Created:** [May 16, 2020, 12:02pm UTC](https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926 "2020-05-16T12:02:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![JavaDeveloper](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javadeveloper/32/67144_2.png) [@JavaDeveloper](https://discuss.elastic.co/u/JavaDeveloper)\
**Post date:** [May 16, 2020, 12:02pm UTC](https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926/1 "2020-05-16T12:02:07Z")

</div>

Hi everyone.  
I'm using stats aggregation (max, min, avg) over some documents with float number fields.  
These float numbers are saved with two decimal point in documents (for example 69.43). But the stats aggregation output corrupts format of float numbers, so that it shows more decimal points.

I'm using java API, so that `org.elasticsearch.search.aggregations.support.ValueType` class includes only `double` format and does not have any option to specify precision.

How can I do that with the elasticsearch?I don't want to do it after manually.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 18, 2020, 5:11pm UTC](https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926/2 "2020-05-18T17:11:21Z")

</div>

May I ask what you mean by "corrupt"?

When you index a document with `float`, even if you only provide two decimal places in the JSON, the internal float may or may not have the same precision. Floats are stored using standard IEEE-754 floating point representation and not all values are able to be explicitly stored. For example, if you try to store the number `12.12` in an IEEE-754 floating point value, internally it is stored as `12.11999988555908203125` because that is the closest arrangement of mantissa and exponent.

This calculator is a convenient way to see how floats are stored: [https://www.h-schmidt.net/FloatConverter/IEEE754.html](https://www.h-schmidt.net/FloatConverter/IEEE754.html)

So that's part of the issue. The other side of the coin is that Elasticsearch aggregations _always_ operate with Doubles, so all values (longs, bytes, shorts, floats, etc) are upconverted to a double. There are technical reasons and historical reasons for this decision, but that's how it is today.

If you only want to _see_ two decimal places of precision, you can use the `format` parameter on the aggregation:

```auto
"format": "00.00"

```

Which should give you only two sigfigs after the decimal (I believe, typed that from memory).

---

<div class="post-metadata">

**Author:** ![JavaDeveloper](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javadeveloper/32/67144_2.png) [@JavaDeveloper](https://discuss.elastic.co/u/JavaDeveloper)\
**Post date:** [May 19, 2020, 8:17am UTC](https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926/3 "2020-05-19T08:17:15Z")

</div>

Thank you for your response.  
But neither of these two method `format("00.00")` and `format("%.2f")` had effect on the result:

```auto
.aggregation(AggregationBuilders.stats("myField").field("my_fileld").format("00.00"))
.aggregation(AggregationBuilders.stats("myField2").field("my_fileld2").format("%.2f"))

```

Therefore, I wrote my own code after getting the results and used `BigDecimal` class to change precesion.

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [May 21, 2020, 3:18pm UTC](https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926/4 "2020-05-21T15:18:32Z")

</div>

Ah, right. Sorry about that, `format` is only applied to the JSON output. When working in Java, we always provide the actual raw value.

Internally `format` just uses [Java's DecimalFormat](https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html) so you could do the same, or use something like BigDecimal.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 18, 2020, 3:18pm UTC](https://discuss.elastic.co/t/elasticsearch-stats-aggregation-corrupt-float-number-precision/232926/5 "2020-06-18T15:18:39Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
