Skip to content

Commit

Permalink
Don't render sum filter results in scientific notation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jg-rp authored and adamklingbaum committed Aug 8, 2023
1 parent 0b93182 commit 2c5d2be
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
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

0 comments on commit 2c5d2be

Please sign in to comment.