diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index b9c6d9c8..a244cc5a 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -243,6 +243,21 @@ module Liquid apply_operation(input, operand, :%) end + def round(input, n = 0) + result = to_number(input).round(to_number(n)) + result = result.to_f if result.is_a?(BigDecimal) + result = result.to_i if n == 0 + result + end + + def ceil(input) + to_number(input).ceil.to_i + end + + def floor(input) + to_number(input).floor.to_i + end + def default(input, default_value = "".freeze) is_blank = input.respond_to?(:empty?) ? input.empty? : !input is_blank ? default_value : input diff --git a/test/integration/standard_filter_test.rb b/test/integration/standard_filter_test.rb index af88f4e2..9486ff6f 100644 --- a/test/integration/standard_filter_test.rb +++ b/test/integration/standard_filter_test.rb @@ -273,6 +273,22 @@ class StandardFiltersTest < Test::Unit::TestCase assert_template_result "1", "{{ 3 | modulo:2 }}" end + def test_round + assert_template_result "5", "{{ input | round }}", 'input' => 4.6 + assert_template_result "4", "{{ '4.3' | round }}" + assert_template_result "4.56", "{{ input | round: 2 }}", 'input' => 4.5612 + end + + def test_ceil + assert_template_result "5", "{{ input | ceil }}", 'input' => 4.6 + assert_template_result "5", "{{ '4.3' | ceil }}" + end + + def test_floor + assert_template_result "4", "{{ input | floor }}", 'input' => 4.6 + assert_template_result "4", "{{ '4.3' | floor }}" + end + def test_append assigns = {'a' => 'bc', 'b' => 'd' } assert_template_result('bcd',"{{ a | append: 'd'}}",assigns)