# Math.round in painless script

**URL:** <https://discuss.elastic.co/t/math-round-in-painless-script/223607>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [March 14, 2020, 9:20am UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607 "2020-03-14T09:20:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Han](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_han/32/45326_2.png) [@David\_Han](https://discuss.elastic.co/u/David_Han)\
**Post date:** [March 14, 2020, 9:20am UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607/1 "2020-03-14T09:20:29Z")

</div>

I have to create a aggs like below:

> 'conversions\_rate' =\> [  
> 'avg' =\> [  
> 'field' =\> 'conversions\_rate',  
> 'script' =\> [  
> 'lang' =\> 'painless',  
> 'inline' =\> 'Math.round(\_value \* params.ten)',  
> 'params' =\> [  
> 'ten' =\> 100  
> ]  
> ],  
> 'missing' =\> 0  
> ]  
> ]

but the Math.round is not work any more. I still get a float.  
How can I fix it ?

---

<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:** [March 16, 2020, 3:46pm UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607/2 "2020-03-16T15:46:14Z")

</div>

Not sure I understand, `Math.round()` requires a float/double as an argument, but returns a long. However the aggregation result will be casted to a float again, if that's what you mean. See this example, of avering two longs

```auto
DELETE test

PUT test/_doc/1
{
  "key" : 10
}

PUT test/_doc/2
{
  "key" : 20
}

GET test/_search
{
  "size": 0,
  "aggs": {
    "avg": {
      "avg": {
        "field": "key"
      }
    }
  }
}

```

Is this what you mean with still getting a float? If so, the float should still be the correct value.

---

<div class="post-metadata">

**Author:** ![David\_Han](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_han/32/45326_2.png) [@David\_Han](https://discuss.elastic.co/u/David_Han)\
**Post date:** [March 18, 2020, 4:01am UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607/3 "2020-03-18T04:01:17Z")

</div>

Thanks a lot. That is what I mean.  
In my mind I thought the Math.round() will return the integer like this ([https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/Math/round](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round))  
before read your reply.

Is this feature that return a float is due to Java's feature?

---

<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:** [March 18, 2020, 1:46pm UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607/4 "2020-03-18T13:46:05Z")

</div>

No, the reason for this is, that aggregation values are always returned as float. See [https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-aggregations.html](https://www.elastic.co/guide/en/elasticsearch/reference/7.6/search-aggregations.html)

---

<div class="post-metadata">

**Author:** ![David\_Han](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_han/32/45326_2.png) [@David\_Han](https://discuss.elastic.co/u/David_Han)\
**Post date:** [March 19, 2020, 12:15pm UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607/5 "2020-03-19T12:15:47Z")

</div>

I see. Thank you so much.

---

<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:** [April 16, 2020, 12:15pm UTC](https://discuss.elastic.co/t/math-round-in-painless-script/223607/6 "2020-04-16T12:15:52Z")

</div>

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