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

Fix BigDecimal output in sum filter #1739

Merged
merged 3 commits into from
Sep 27, 2023

Conversation

adamklingbaum
Copy link
Contributor

@adamklingbaum adamklingbaum commented Aug 8, 2023


Thank you, @jg-rp, for identifying this bug and helping to implement the fix.

Summary

This PR addresses a bug in the sum filter when handling arrays containing floats and thus yield a result that is a float.

Previously, the sum filter would return a BigDecimal, which would render in scientific notation, when summing floats. This behaviour was inconsistent with other math filters.

The change in this PR ensures that the sum filter returns a float instead of a BigDecimal when summing floats, aligning its behaviour with other math filters.

For example:

def apply_operation(input, operand, operation)
result = Utils.to_number(input).send(operation, Utils.to_number(operand))
result.is_a?(BigDecimal) ? result.to_f : result
end

Changes

  • Updated the sum filter in lib/liquid/standardfilters.rb to convert BigDecimal results to floats when summing floats.
  • Added tests in test/integration/standard_filter_test.rb to verify the correct behaviour of the sum filter when handling floats.

@adamklingbaum adamklingbaum force-pushed the klingbaum/fix-sum-filter-float-output branch from 841c840 to 75e7725 Compare August 8, 2023 16:16
@adamklingbaum adamklingbaum changed the title Fix sum filter BigDecimal output Fix BigDecimal output in sum filter Aug 8, 2023
@adamklingbaum adamklingbaum marked this pull request as ready for review August 8, 2023 16:36
@Czx00000 Czx00000 mentioned this pull request Sep 12, 2023
@adamklingbaum adamklingbaum merged commit b1b9b9f into main Sep 27, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Summing floats with the sum filter
3 participants