# Convert float to int in a scripted field

**URL:** <https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400>\
**Category:** Kibana\
**Created:** [August 15, 2019, 10:06pm UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400 "2019-08-15T22:06:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcaballero](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jcaballero](https://discuss.elastic.co/u/jcaballero)\
**Post date:** [August 15, 2019, 10:06pm UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/1 "2019-08-15T22:06:23Z")

</div>

Hi,

using Kibana 5.4.1

My documents have 2 date-type fields: "starttime" and "endtime". I am trying to create a scripted field with the difference, in hours.  
In the script box, I have this

```
(doc['endtime'].value - doc['starttime'].value)/3600

```

but it gives me the result as a float. For example: 24.000  
How can I cast it to an integer? I don't find a clear clue in the documentation...

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [August 15, 2019, 10:27pm UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/2 "2019-08-15T22:27:30Z")

</div>

Are these actual date type fields in Elasticsearch? If so, [this doc page](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-datetime.html) will provide you with a lot of information about working with dates in Painless scripts.

---

<div class="post-metadata">

**Author:** ![jcaballero](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jcaballero](https://discuss.elastic.co/u/jcaballero)\
**Post date:** [August 15, 2019, 10:29pm UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/3 "2019-08-15T22:29:49Z")

</div>

indeed they are.  
I had a look before to that page, and I didn't see anything clear that can help me. I will try again, maybe I missed it... 😛

---

<div class="post-metadata">

**Author:** ![Bargs](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bargs/32/5429_2.png) [@Bargs](https://discuss.elastic.co/u/Bargs)\
**Post date:** [August 15, 2019, 10:53pm UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/4 "2019-08-15T22:53:04Z")

</div>

Try this:

```auto
def start = doc['starttime'].value;
def end = doc['endtime'].value;
return ChronoUnit.HOURS.between(start, end);

```

---

<div class="post-metadata">

**Author:** ![jcaballero](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jcaballero](https://discuss.elastic.co/u/jcaballero)\
**Post date:** [August 15, 2019, 11:31pm UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/5 "2019-08-15T23:31:49Z")

</div>

AFK.  
I will try that, and I will provide for feedback.

In any case, even if that resolves this particular case, the question is more general: how to cast a float to int.

---

<div class="post-metadata">

**Author:** ![jcaballero](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jcaballero](https://discuss.elastic.co/u/jcaballero)\
**Post date:** [August 16, 2019, 1:24am UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/6 "2019-08-16T01:24:23Z")

</div>

Hmmm. Everything seems to be a little bit of a confusion. The output I got -24,000- was not a float. Apparently it is an integer, 24000. It seems like doc['foo'].value returns milliseconds.  
I guess I got confused with the format for decimals (dot vs comma).

---

<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 13, 2019, 1:24am UTC](https://discuss.elastic.co/t/convert-float-to-int-in-a-scripted-field/195400/7 "2019-09-13T01:24:28Z")

</div>

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