# Logstash Ruby not handling float value correctly

**URL:** https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150
**Category:** Logstash
**Created:** [October 23, 2020, 2:35pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150 "2020-10-23T14:35:40Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)
#### Post date: [October 23, 2020, 2:35pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/1 "2020-10-23T14:35:41Z")

</div>

I have Kafak \<\> Logstash (then Ruby) \<\> Elastic

Kafak Input:

```auto
{"customerid":"smit","last_name":"shah","age":10,"height":10,"weight":100,"automated_email":false, "header": { "endpoint":"/pay"}, "transaction": { "amount":100.50, "currency" : "SGD"}}

```

Logstash:

```auto
 json {
        source => "message"
      }

 mutate {
        convert => { "amount" => "float" }
    }
	ruby {
        # Cancel 90% of events
        path => "/usr/local/etc/logstash/main.rb"
        script_params => { "percentage" => 0.9 }
      }
    }

```

Ruby

```auto
transaction = event.get('transaction')
		puts transaction
		puts transaction['amount']

```

This prints:

```auto
{"amount"=>0.1005e3, "currency"=>"SGD"}
0.1005e3

```

Why I am not getting 100.5?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [October 23, 2020, 3:35pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/2 "2020-10-23T15:35:07Z")

</div>

> [@mastersmit](#):
>
> Why I am not getting 100.5?

ruby likes to print floats in exponential form. You can tell it not to using sprintf formatting strings such as %5.1f

---

<div class="post-metadata">

### Author: ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)
#### Post date: [October 23, 2020, 5:04pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/3 "2020-10-23T17:04:36Z")

</div>

Thanks. Thats solved the issue partially.

I have added this: `amazing_var = sprintf('%f',transaction['amount'])`

It is printing `100.500000` Is it possible for it to not format to padding zero and keep exact value.

Note: It is not known the no. of decimal points in the input.

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [October 23, 2020, 5:11pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/4 "2020-10-23T17:11:32Z")

</div>

I suggest you use mutate+gsub to remove the trailing zeroes.

---

<div class="post-metadata">

### Author: ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)
#### Post date: [October 25, 2020, 1:09pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/5 "2020-10-25T13:09:47Z")

</div>

@Badger Here is the code I am planning on using, do you suggest any better way to write this:

```auto
amount = "1234.50000"

def filtered_amount(amount)
    decimal_amt = amount.split('.', -1)[1]
    after_index = -1
    for i in 0..decimal_amt.length - 1
        current_char = decimal_amt[i]
        if current_char == '0' && after_index == -1
            after_index = i
        elsif current_char != '0'
            after_index = -1
        end
    end
    first_part = decimal_amt[0..after_index]
    return amount.split('.', -1)[0] << "." << first_part
end

puts "Before: " << amount

puts "After: " << filtered_amount(amount)

```

Output:

```auto
$ruby main.rb
Before: 1234.50000
After: 1234.50

```

As I believe this will take up too much of processing time. Any Suggestion?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [October 25, 2020, 2:15pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/6 "2020-10-25T14:15:43Z")

</div>

If you always want two digits after the period I would use mutate, not ruby

```
mutate { gsub => ["amount", "(\.\d\d)0+", "\1"] }
```

---

<div class="post-metadata">

### Author: ![mastersmit](https://avatars.discourse-cdn.com/v4/letter/m/f475e1/32.png) [@mastersmit](https://discuss.elastic.co/u/mastersmit)
#### Post date: [October 25, 2020, 3:31pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/7 "2020-10-25T15:31:07Z")

</div>

Nope, my usecase does not have a fixed length after `dot`. However thanks for the code snippet. Appreciate it.

---

<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: [November 22, 2020, 3:31pm UTC](https://discuss.elastic.co/t/logstash-ruby-not-handling-float-value-correctly/253150/8 "2020-11-22T15:31:10Z")

</div>

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