Skip to content

Summing floats with the sum filter #1725

@jg-rp

Description

@jg-rp

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions