# How to format sum\_bucket aggregation float output?

**URL:** <https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851>\
**Category:** Elasticsearch\
**Created:** [January 10, 2022, 7:16am UTC](https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851 "2022-01-10T07:16:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eduardo\_Montes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eduardo_montes/32/100049_2.png) [@Eduardo\_Montes](https://discuss.elastic.co/u/Eduardo_Montes)\
**Post date:** [January 10, 2022, 7:16am UTC](https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851/1 "2022-01-10T07:16:49Z")

</div>

I use Elasticsearch for my Search Service and I use this JSON request for sum\_bucket aggregation:

```auto
"from": 0,
    "size": 0,
    "aggregations": {
        "aggr": {
            "type": "term",
            "field": "type.keyword",
            "size": 10,
            "order": {
                "myAggr2.value": "desc"
            },
            "aggregations": {
                "myAggr2": {
                    "type": "sum",
                    "field": "length"
                }
            }
        },
        "total_myAggr2": {
            "type": "sum_bucket",
            "buckets_path": "aggr>myAggr2"
        }
    },
    "types": [
        "cable"
    ]
}

```

In the response I get this:

```auto
{
    "totalHits": 4,
    "tookInMillis": 1,
    "hits": [],
    "aggregations": {
        "total_myAggr2": {
            "value": "3.7600000000000002"
        },
        "aggr": {
            "sumOtherDocCount": 0,
            "buckets": [
                {
                    "key": "cable",
                    "count": 4,
                    "aggregations": {
                        "myAggr2": {
                            "value": "3.7600000000000002"
                        }
                    }
                }
            ]
        }
    }
}

```

Is any possibility to format the output from `"3.7600000000000002"` to `"3.76"` ? I found attribute **format** in ES docu but not for number formatting...

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 10, 2022, 8:22am UTC](https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851/2 "2022-01-10T08:22:32Z")

</div>

Hi,  
I found format attribute described in [Average Bucket Aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-avg-bucket-aggregation.html) of the document. It says the format is according to[Java DecimalFormat](https://docs.oracle.com/javase/jp/8/docs/api/java/text/DecimalFormat.html): `format: "#.00"`.

(PS I incidentaly found the `format` attribute are usable in `sum aggregation` too.)

---

<div class="post-metadata">

**Author:** ![Eduardo\_Montes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eduardo_montes/32/100049_2.png) [@Eduardo\_Montes](https://discuss.elastic.co/u/Eduardo_Montes)\
**Post date:** [January 10, 2022, 12:51pm UTC](https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851/3 "2022-01-10T12:51:58Z")

</div>

Thank you @Tomo_M! I have one more question... 🙂 Why is the result both aggregations in float format? Because I have created the index for field **length** with scaled\_float=100 so I would expected the result as `"3.76"`.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 10, 2022, 1:16pm UTC](https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851/4 "2022-01-10T13:16:32Z")

</div>

I'm only a user and not sure about the internal argorithm about scaled\_float, but I suppose aggregation uses only its value as float regardless of numeric field types because even aggregation on integer field returns float as follows.  
My guess is that the conversion to float is done **before** the aggregation, which is the cause of your rounding error. Note that this is just my guess 😀

```auto
{
"aggregations" : {
    "my_sum" : {
      "value" : 85425.0
    }
  }
}

```

---

<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:** [February 7, 2022, 1:16pm UTC](https://discuss.elastic.co/t/how-to-format-sum-bucket-aggregation-float-output/293851/5 "2022-02-07T13:16:39Z")

</div>

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