# Painscript Script - cast String as Int?

**URL:** <https://discuss.elastic.co/t/painscript-script-cast-string-as-int/97034>\
**Category:** Kibana\
**Created:** [August 14, 2017, 11:13pm UTC](https://discuss.elastic.co/t/painscript-script-cast-string-as-int/97034 "2017-08-14T23:13:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daflookie](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@daflookie](https://discuss.elastic.co/u/daflookie)\
**Post date:** [August 14, 2017, 11:13pm UTC](https://discuss.elastic.co/t/painscript-script-cast-string-as-int/97034/1 "2017-08-14T23:13:32Z")

</div>

We have a mapping issue that will be addressed soon for a field that should be an int but is presently a string. All values in the index should cast cleanly.

Anyhow, in a pinch I wanted to aggregate some data on the field and thought creating a scripted field in K5 would be a temp solution. Can anyone point me in the right direction or provide a code example? I tried several things including `(int)['doc.myfield'].value` and also `parseInt(string)` as documented at [https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt%2Djava.lang.String%2D](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#parseInt%2Djava.lang.String%2D).

Is type casting a String to Int supported in either of the 3 scripting languages listed in K5?

---

<div class="post-metadata">

**Author:** ![chrisronline](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chrisronline/32/28230_2.png) [@chrisronline](https://discuss.elastic.co/u/chrisronline)\
**Post date:** [August 15, 2017, 7:09pm UTC](https://discuss.elastic.co/t/painscript-script-cast-string-as-int/97034/2 "2017-08-15T19:09:49Z")

</div>

Hi Steve,

Try:

```auto
Integer.parseInt(doc['myfield'].value)

```

Thanks

---

<div class="post-metadata">

**Author:** ![daflookie](https://avatars.discourse-cdn.com/v4/letter/d/46a35a/32.png) [@daflookie](https://discuss.elastic.co/u/daflookie)\
**Post date:** [August 15, 2017, 8:14pm UTC](https://discuss.elastic.co/t/painscript-script-cast-string-as-int/97034/3 "2017-08-15T20:14:20Z")

</div>

That worked like a champ - thanks Chris!

---

<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 12, 2017, 8:14pm UTC](https://discuss.elastic.co/t/painscript-script-cast-string-as-int/97034/4 "2017-09-12T20:14:24Z")

</div>

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