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

Don't render sum filter results in scientific notation. #1728

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/liquid/standardfilters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,10 @@ def sum(input, property = nil)
raise_property_error(property)
end

InputIterator.new(values_for_sum, context).sum do |item|
result = InputIterator.new(values_for_sum, context).sum do |item|
Utils.to_number(item)
end
result.is_a?(BigDecimal) ? result.to_f : result
end

private
Expand Down
31 changes: 31 additions & 0 deletions test/integration/standard_filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,21 @@ def test_sum_with_numeric_strings
end
end

def test_sum_with_floats
input = [0.1, 0.2, 0.3]
assert_equal(0.6, @filters.sum(input))
end

def test_sum_with_negative_float
input = [0.1, 0.2, -0.3]
assert_equal(0.0, @filters.sum(input))
end

def test_sum_with_float_strings
input = [0.1, "0.2", "0.3"]
assert_equal(0.6, @filters.sum(input))
end

def test_sum_with_nested_arrays
input = [1, [2, [3, 4]]]

Expand Down Expand Up @@ -994,6 +1009,22 @@ def test_sum_with_property_calls_to_liquid_on_property_values
assert(t.foo > 0)
end

def test_render_sum_of_floats
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, 0.3]), "0.6")
end

def test_render_sum_of_negative_floats
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, 0.3]), "0.2")
end

def test_render_sum_negative_float_result
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, -0.2, -0.3]), "-0.4")
end

def test_render_sum_float_zero_result
assert_equal(Liquid::Template.parse('{{ foo | sum }}').render("foo" => [0.1, 0.2, -0.3]), "0.0")
end

private

def with_timezone(tz)
Expand Down