Shopify / liquid

Liquid markup language. Safe, customer facing template language for flexible web apps.

Home Page:https://shopify.github.io/liquid/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Summing floats with the `sum` filter

jg-rp opened this issue · comments

commented

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.