# Specify the number of decimal places on an aggregate average

**URL:** https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [July 10, 2017, 10:34am UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449 "2017-07-10T10:34:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![slarsendisney](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/slarsendisney/32/19892_2.png) [@slarsendisney](https://discuss.elastic.co/u/slarsendisney)
#### Post date: [July 10, 2017, 10:34am UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449/1 "2017-07-10T10:34:20Z")

</div>

Within my input on one of my watches I have :

> ```
> "aggs": {
> "e2e_av_time": {
> "avg": {
> "field" : "e2e_time"
> }
> }
> 
> ```

> (Note that e2e\_time is a long within my mapping.)  
> I then use it later in the action:  
> `> "text": "The average e2e time has dropped to {{ctx.payload.aggregations.e2e_av_time.value}}ms"`

However in some cases this is producing 8 decimal places from whole integer numbers. Simulation Output:

> "text": "The average e2e time has dropped to 313.922512715544ms"`

The maximum number of decimal places I would want is 2 and preferably none! In order to fix this I tried to use a script :

> ```
> "aggs": {
> "e2e_av_time": {
> "avg": {
> "script": {
> "inline": "return (int) Math.ceil(doc['e2e_time'].value)"
> }
> }
> }
> }
> 
> ```

I have also tried variations without casting and with Math.round() and .round(). I'm hoping that someone can tell me where I'm going wrong? Am i pointing to the wrong area of the payload? Thanks in advance.

---

<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: [July 10, 2017, 11:45am UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449/2 "2017-07-10T11:45:41Z")

</div>

Hey,

you can use `DecimalFormat` to format numbers properly. The correct position to do this, should be in a transform before your action is executed, do not do this as a script in your search, as this will decrease performance.

```auto
new java.text.DecimalFormat('#.##').format(3.151429)

```

Hope this helps!

--Alex

---

<div class="post-metadata">

### Author: ![slarsendisney](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/slarsendisney/32/19892_2.png) [@slarsendisney](https://discuss.elastic.co/u/slarsendisney)
#### Post date: [July 10, 2017, 12:16pm UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449/3 "2017-07-10T12:16:19Z")

</div>

Thanks for your help. How is this used within transform? Like a script? Could you give me an example transform using this decimal format?

---

<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: [July 10, 2017, 1:49pm UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449/4 "2017-07-10T13:49:51Z")

</div>

Sorry for not being clear. I was referring to a [script transform](https://www.elastic.co/guide/en/x-pack/5.5/transform-script.html) where you return a formatted value .

--Alex

---

<div class="post-metadata">

### Author: ![slarsendisney](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/slarsendisney/32/19892_2.png) [@slarsendisney](https://discuss.elastic.co/u/slarsendisney)
#### Post date: [July 12, 2017, 11:59am UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449/5 "2017-07-12T11:59:32Z")

</div>

Thanks for your help!

---

<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: [August 9, 2017, 12:00pm UTC](https://discuss.elastic.co/t/specify-the-number-of-decimal-places-on-an-aggregate-average/92449/6 "2017-08-09T12:00:04Z")

</div>

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