# Rounding issues

**URL:** <https://discuss.elastic.co/t/rounding-issues/95563>\
**Category:** Elasticsearch\
**Tags:** elastic-stack-alerting\
**Created:** [August 2, 2017, 3:14pm UTC](https://discuss.elastic.co/t/rounding-issues/95563 "2017-08-02T15:14:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Tomasheski](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@David\_Tomasheski](https://discuss.elastic.co/u/David_Tomasheski)\
**Post date:** [August 2, 2017, 3:14pm UTC](https://discuss.elastic.co/t/rounding-issues/95563/1 "2017-08-02T15:14:46Z")

</div>

I have a watch that performs an average on a value. I would like to round it to two decimal places. For some reason when I put the script in to round the result is wrong, plus it doesn't even round. In the below code avt\_tv comes out to 1114.873063170441 while avg\_tv\_round comes out to 560.9466266866567. What am I doing wrong?

```
"aggs": {
        "avg_tv_round": {
          "avg": {
            "script": {
              "inline": "Math.round(doc['TESTVALUE'].value * 100.0)/100.0"
            }
          }
        },
        "avg_tv": {
          "avg": {
            "field": "TESTVALUE"
          }
        }
      }
```

---

<div class="post-metadata">

**Author:** ![David\_Tomasheski](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@David\_Tomasheski](https://discuss.elastic.co/u/David_Tomasheski)\
**Post date:** [August 2, 2017, 6:04pm UTC](https://discuss.elastic.co/t/rounding-issues/95563/2 "2017-08-02T18:04:54Z")

</div>

So I figured out why one of my issues is happening. The avt\_tv\_round is dfferent because I have entries without the TESTVALUE field. According to the document I read on the avg aggreation missing values are supposed to be ignored but it seems that it is still counting those entries into the count for the avg divisor i.e. 3 of my 4 entries have TESTVALUE but after adding them together it divides by 4 instead of 3.

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 3, 2017, 7:11am UTC](https://discuss.elastic.co/t/rounding-issues/95563/3 "2017-08-03T07:11:20Z")

</div>

You could circumvent this using a value script as you have to specify a field, see [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-aggregations-metrics-avg-aggregation.html#\_value\_script](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-aggregations-metrics-avg-aggregation.html#_value_script)

However scripting still means, that a script has to be executed for every document that is found, so just using this for rounding is highly inefficeent. Might be less resource intensive to just do this on the client side.

---

<div class="post-metadata">

**Author:** ![David\_Tomasheski](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@David\_Tomasheski](https://discuss.elastic.co/u/David_Tomasheski)\
**Post date:** [August 3, 2017, 2:11pm UTC](https://discuss.elastic.co/t/rounding-issues/95563/4 "2017-08-03T14:11:08Z")

</div>

That circumvented the incorrect average problem but still the other problem the fact that it isn't rounding the result my updated code is below. Is there perhaps some setting I need to set in the elasticsearch.yml that I am unaware of? Or is it possible to round in the condition field(i tried but keep getting null back)

```
"avg_tv_round" : {
        "avg" : {
            "field" : "TESTVALUE",
            "script" : {
                "lang": "painless",
                "inline": "Math.round(_value * params.convertor)/params.convertor",
                "params" : {
                    "convertor" : 100.0
                }
            }
        }
    }
```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [August 4, 2017, 6:16am UTC](https://discuss.elastic.co/t/rounding-issues/95563/5 "2017-08-04T06:16:56Z")

</div>

hey,

you could use `NumberFormat`/`DecimalFormat` to format, but this also converts to a string and you need to return a number here, because the average still needs to be calculated (which will be skewed as well).

--Alex

---

<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:** [September 1, 2017, 6:17am UTC](https://discuss.elastic.co/t/rounding-issues/95563/6 "2017-09-01T06:17:08Z")

</div>

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