Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.
https://shopify.github.io/liquid/
MIT License
11.13k stars 1.39k forks source link

Summing floats with the `sum` filter #1725

Closed jg-rp closed 1 year ago

jg-rp commented 1 year ago

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?

https://github.com/Shopify/liquid/blob/77293d4524c55471dd4268ba3a536031c39ee20e/lib/liquid/standardfilters.rb#L791

Many thanks.