Compare commits

...

1 Commits

Author SHA1 Message Date
Bimal Bhagrath
bfc6f61563 Add round_to_s filter with tests 2019-05-16 14:10:09 -04:00
2 changed files with 12 additions and 0 deletions

View File

@@ -391,6 +391,11 @@ module Liquid
raise Liquid::FloatDomainError, e.message
end
def round_to_s(input, n = 0)
result = Utils.to_number(input)
sprintf("%.#{n}f", result)
end
def ceil(input)
Utils.to_number(input).ceil.to_i
rescue ::FloatDomainError => e

View File

@@ -610,6 +610,13 @@ class StandardFiltersTest < Minitest::Test
assert_template_result "4", "{{ price | round }}", 'price' => NumberLikeThing.new(4.3)
end
def test_round_to_s
assert_template_result "5", "{{ input | round_to_s }}", 'input' => 4.6
assert_template_result "4.600", "{{ input | round_to_s:3 }}", 'input' => 4.6
assert_template_result "Inf", "{{ 1.0 | divided_by: 0.0 | round_to_s }}"
assert_template_result "5", "{{ price | round_to_s }}", 'price' => NumberLikeThing.new(4.6)
end
def test_ceil
assert_template_result "5", "{{ input | ceil }}", 'input' => 4.6
assert_template_result "5", "{{ '4.3' | ceil }}"