# Scripted Field Percentage Calculation

**URL:** <https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721>\
**Category:** Kibana\
**Created:** [August 16, 2018, 1:00pm UTC](https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721 "2018-08-16T13:00:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MichaelN](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelN](https://discuss.elastic.co/u/MichaelN)\
**Post date:** [August 16, 2018, 1:00pm UTC](https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721/1 "2018-08-16T13:00:13Z")

</div>

Hi all,

I am trying to make a % calculation based on two fields.

I made the following script:

if (doc['doc.contextMap.totalErrors.keyword'].value != null && doc['doc.contextMap.totalInput.keyword'].value != null) {  
return (Integer.parseInt(doc['doc.contextMap.totalErrors.keyword'].value) / Integer.parseInt(doc['doc.contextMap.totalInput.keyword'].value)) \* 100;  
} else {  
return -100;  
}

The outcome is always 0. It pass the IF condition but the calculation is always 0.

Any suggestions on what I am doing wrong?

Thanks

---

<div class="post-metadata">

**Author:** ![a5a](https://avatars.discourse-cdn.com/v4/letter/a/a87d85/32.png) [@a5a](https://discuss.elastic.co/u/a5a)\
**Post date:** [August 16, 2018, 4:11pm UTC](https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721/2 "2018-08-16T16:11:30Z")

</div>

`null` and `0` are different values. It may be that the `doc['doc.contextMap.totalErrors.keyword'].value` is 0. Can you verify?

---

<div class="post-metadata">

**Author:** ![MichaelN](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelN](https://discuss.elastic.co/u/MichaelN)\
**Post date:** [August 17, 2018, 5:38pm UTC](https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721/3 "2018-08-17T17:38:11Z")

</div>

Hi,

There are a values in totalErrors. Now I am thinking about another problem, may be I need to cast it into float and not an integer. What should be the syntax to do it?

Thanks.

---

<div class="post-metadata">

**Author:** ![MichaelN](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@MichaelN](https://discuss.elastic.co/u/MichaelN)\
**Post date:** [August 17, 2018, 5:53pm UTC](https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721/4 "2018-08-17T17:53:11Z")

</div>

Indeed the problem was in casting to integer instead of float.

Here is the correct script that work for me

if (doc['doc.contextMap.totalErrors.keyword'].value != null && doc['doc.contextMap.totalInput.keyword'].value != null) {  
return (Float.parseFloat(doc['doc.contextMap.totalErrors.keyword'].value) / Float.parseFloat(doc['doc.contextMap.totalInput.keyword'].value));  
} else {  
return -100;  
}

---

<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 14, 2018, 6:03pm UTC](https://discuss.elastic.co/t/scripted-field-percentage-calculation/144721/5 "2018-09-14T18:03:45Z")

</div>

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