elastic / elastic/logstash

Ruby Filter Plugin - Float conversion to_s() and round() discrepancies 1.4.2->2.3.3

Open
#5,631 1 comment 0 reactions 1 assignee Claimed by @purbon View on GitHub
bug
Dominant language
Java
Stars
14.9k
Forks
3.5k
Avg merge
1d 4h
Merged PRs (30d)
88

Description

Background:
- Upgrading from Logstash 1.4.2 to 2.3.3
- Using the Ruby filter, and in it I'm trying to convert a float to a string from the `event`.

Two differences between versions:
1. When using `to_s`, 2.3.3 changes the output to scientific notation, which I cannot duplicate on any ruby interpreter.
2. I'm seeing different behavior from `.round()` compared to Ruby specification `.round()` vs `.round(0)` should have the same output according to: http://ruby-doc.org/core-2.1.2/Float.html#method-i-round 2.3.3 does not show this behavior.

Ruby Code in the filter:

``` ruby
event["float_num"] = event["originalFloat"]
event["float_as_str_1"] = sprintf("%.2f", event["float_as_str"])
event["float_as_str_2"] = event["float_as_str"].round()
event["float_as_str_3"] = event["float_as_str"].round().to_s
event["float_as_str_4"] = event["float_as_str"].round(0)
event["float_as_str_5"] = event["float_as_str"].round(0).to_s
```

Output using rubydebug:

``` ruby
"float_num" => 103836.9172733573849,
"float_as_str_1" => "103836.92",
"float_as_str_2" => 103837,
"float_as_str_3" => "103837",
"float_as_str_4" => 103837.0,
"float_as_str_5" => "0.103837E6",
```

Do these two differences have something to do with Logstash's JRuby implementation/version?

I'm going to settle with the `sprintf` for 2.3.3, however I thought the differences were strange enough to create an issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.