Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Summing floats with the sum filter #1725

Closed
jg-rp opened this issue Jun 20, 2023 · 0 comments · Fixed by #1739 · May be fixed by #1728
Closed

Summing floats with the sum filter #1725

jg-rp opened this issue Jun 20, 2023 · 0 comments · Fixed by #1739 · May be fixed by #1728

Comments

@jg-rp
Copy link
Contributor

jg-rp commented Jun 20, 2023

When using the sum filter on an array containing floats, we get a BigDecimal result, which is rendered in scientific notation.

require 'liquid'

template = Liquid::Template.parse("Result: {{ a | sum }}")
puts template.render!({"a" => [0.1, 0.2, 0.3]})

Output

Result: 0.6e0

Expected output

Result: 0.6

For consistency with existing math filters, would it be possible to convert a BigDecimal result to a float, just like the round filter does?

result = result.to_f if result.is_a?(BigDecimal)

Many thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant