# Percentage in scripted fields

**URL:** <https://discuss.elastic.co/t/percentage-in-scripted-fields/262489>\
**Category:** Kibana\
**Created:** [January 28, 2021, 11:35am UTC](https://discuss.elastic.co/t/percentage-in-scripted-fields/262489 "2021-01-28T11:35:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [January 28, 2021, 11:35am UTC](https://discuss.elastic.co/t/percentage-in-scripted-fields/262489/1 "2021-01-28T11:35:59Z")

</div>

Hi

I have divided two fields using below script

```auto
return doc['StatusOpen'].value/doc['StatusSent'].value

```

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/b/8b14b0de53f516f6eabea67aa8ae82ab91f93c49.png)

for this result should be 2/31 = 0.06451 = 6.451% but it's giving 0 %

As I have format as percentage and Numeral.js format pattern as 0,0.[000]%

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/f/4f0e29f832f502ad48d5bf4561e068ca04765b2c.png)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/f/af2ffd3b97ee82ef756a2a84794e21144de397e1.png)

May I know where I went wrong.

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [January 28, 2021, 12:26pm UTC](https://discuss.elastic.co/t/percentage-in-scripted-fields/262489/2 "2021-01-28T12:26:25Z")

</div>

`StatusOpen` and `StatusSent` are probably of type `int` - in this case the `/` operator is doing an integer division. By casting them to double it should work as expected ([https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-casting.html](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-casting.html) ):  
`return ((double) doc['StatusOpen'].value)/((double) doc['StatusSent'].value)`

---

<div class="post-metadata">

**Author:** ![Pranusha119](https://avatars.discourse-cdn.com/v4/letter/p/47e85d/32.png) [@Pranusha119](https://discuss.elastic.co/u/Pranusha119)\
**Post date:** [January 28, 2021, 12:38pm UTC](https://discuss.elastic.co/t/percentage-in-scripted-fields/262489/3 "2021-01-28T12:38:53Z")

</div>

Awesome!! it's working @flash1293

---

<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 25, 2021, 12:38pm UTC](https://discuss.elastic.co/t/percentage-in-scripted-fields/262489/4 "2021-02-25T12:38:55Z")

</div>

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