Get Integer part of float number and round

Hello all,

how can extract integer part of a float field with logstash

"field_fload" => 1.4573624

i want to get 1, only the integer part of the float number.

And how can round up the float?

"field_fload" => 0.923454

and get a 1


I would do it using ruby

ruby { code => 'event.set("field_fload", even.get("field_fload").floor)' }

will round down. If you want to round up use ceil instead of floor. If you want to round to the nearest even number you could use

ruby { code => 'event.set("field_fload", even.get("field_fload").(half: :even))' }

Thank you badger, you are the best

Hello Badger,

is this syntax correct?- logstash throws an error

ruby { code => 'event.set("field_fload", even.get("field_fload").(half: :even))' }


That should be event.get

Hello Badger,

trying you solution for getting only the integer part of a float is not working for me, i don't know what i am doing wrong.

ruby { code => 'event.set("field_fload", event.get("my_field").(half: :even))' }

I am getting this error:

Ruby exception ocurred: no implicit conversion of Float into String
Ruby exception ocurred: undefined method 'call' for -0.1456345_Float

Sorry about that, another typo in my answer. Try

ruby { code => 'event.set("field_fload", event.get("field_fload").round(half: :even))' }

Thanks Badger, now is working but...

Is rounding up:

"field_value_integer_part" => 5
"field_to_extract_integer_part" => 4.77

The integer should be 4 instead of 5

I am unable to explain why .round(half: :even) would round to an odd number.

No matter Badger, i am going to investigate. Thanks for your help!

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