# How to round the sum of metric value on Scripted Field in Visualization?

**URL:** <https://discuss.elastic.co/t/how-to-round-the-sum-of-metric-value-on-scripted-field-in-visualization/110465>\
**Category:** Kibana\
**Created:** [December 6, 2017, 7:20am UTC](https://discuss.elastic.co/t/how-to-round-the-sum-of-metric-value-on-scripted-field-in-visualization/110465 "2017-12-06T07:20:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shalvin\_Kumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shalvin_kumar/32/24142_2.png) [@Shalvin\_Kumar](https://discuss.elastic.co/u/Shalvin_Kumar)\
**Post date:** [December 6, 2017, 7:20am UTC](https://discuss.elastic.co/t/how-to-round-the-sum-of-metric-value-on-scripted-field-in-visualization/110465/1 "2017-12-06T07:20:59Z")

</div>

Hello community,

I have written a Scripted field where I am getting P&L of successful trades in a field that I'll later use in a visualization (sum of P&L of successful trades).

Code in painless:

```
if(doc['Type.keyword'].value == "F")
{
    return Math.round(doc['Cont P&L'].value * 100.0)/100.0;
} else {
    return 0;
}

```

Now when I am making a visualization where I am adding all the P&L from this field. And I get this value :  
-4582.040000000001

However, I am looking at a value -4582.04.

Can you guys please help me this issue? Thanks 🙂

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [December 6, 2017, 5:08pm UTC](https://discuss.elastic.co/t/how-to-round-the-sum-of-metric-value-on-scripted-field-in-visualization/110465/2 "2017-12-06T17:08:01Z")

</div>

Hi Shalvin,

I think your problem with rounding is that your parenthesis grouping for Math.round isn't including the `/100.0`. So you're taking your value \* 100 and rounding that (rounding -458204.0000001 to -458204) and then dividing that by 100 which gives you -4582.04.

So I think this would fix it;  
`return Math.round((doc['Cont P&L'].value * 100.0)/100.0);`

of even simpler  
`return Math.round(doc['Cont P&L'].value);`

You can also use the format option in your scripted field to set the format to `1000` that would tell it to not show any decimal places or thousands separator. The advantage of only doing the formatting is that if you do a sum or average of the field then the end result might be more accurate.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/1/9/191095ee7c33cd208879440e78da0b846c47c2e7.png)

Regards,  
Lee

---

<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:** [January 3, 2018, 5:08pm UTC](https://discuss.elastic.co/t/how-to-round-the-sum-of-metric-value-on-scripted-field-in-visualization/110465/3 "2018-01-03T17:08:47Z")

</div>

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