mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 19:00:39 -07:00
Follow ISO recommendation to use space as thousands separator
This commit is contained in:
@@ -391,7 +391,11 @@ module Liquid
|
|||||||
raise Liquid::FloatDomainError, e.message
|
raise Liquid::FloatDomainError, e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
def format(input, n = 2, thousands = ',', decimal = '.')
|
# Defaults are passed as nil so systems can easily override
|
||||||
|
def format(input, n = nil, thousands = nil, decimal = nil)
|
||||||
|
n = 2 if n.nil?
|
||||||
|
thousands = " ".freeze if thousands.nil?
|
||||||
|
decimal = ".".freeze if decimal.nil?
|
||||||
return input if (precision = Utils.to_number(n).to_i) < 0
|
return input if (precision = Utils.to_number(n).to_i) < 0
|
||||||
whole_part, decimal_part = Kernel.format("%.#{precision}f", Utils.to_number(input)).split('.')
|
whole_part, decimal_part = Kernel.format("%.#{precision}f", Utils.to_number(input)).split('.')
|
||||||
[whole_part.gsub(/(\d)(?=\d{3}+$)/, "\\1#{thousands}"), decimal_part].compact.join(decimal.to_s)
|
[whole_part.gsub(/(\d)(?=\d{3}+$)/, "\\1#{thousands}"), decimal_part].compact.join(decimal.to_s)
|
||||||
|
|||||||
@@ -619,12 +619,13 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_template_result "4.30", "{{ price | format: 2 }}", 'price' => NumberLikeThing.new(4.3)
|
assert_template_result "4.30", "{{ price | format: 2 }}", 'price' => NumberLikeThing.new(4.3)
|
||||||
assert_template_result "5.0000000", "{{ price | format: 7 }}", 'price' => 5
|
assert_template_result "5.0000000", "{{ price | format: 7 }}", 'price' => 5
|
||||||
assert_template_result "50", "{{ price | format: -1 }}", 'price' => 50
|
assert_template_result "50", "{{ price | format: -1 }}", 'price' => 50
|
||||||
assert_template_result "50", "{{ price | format: A }}", 'price' => 50
|
assert_template_result "50.00", "{{ price | format: A }}", 'price' => 50
|
||||||
assert_template_result "50.00", "{{ price | format: '2e' }}", 'price' => 50
|
assert_template_result "50.00", "{{ price | format: '2e' }}", 'price' => 50
|
||||||
assert_template_result "50,000,000", "{{ price | format: 0 }}", 'price' => 50000000
|
assert_template_result "50 000 000", "{{ price | format: 0 }}", 'price' => 50000000
|
||||||
assert_template_result "50,000,000.00", "{{ price | format }}", 'price' => 50000000
|
assert_template_result "50 000 000.00", "{{ price | format }}", 'price' => 50000000
|
||||||
assert_template_result "50000000.00", "{{ price | format: 2, '', '.'}}", 'price' => 50000000
|
assert_template_result "50000000.00", "{{ price | format: 2, '', '.'}}", 'price' => 50000000
|
||||||
assert_template_result "50$000$000#00", "{{ price | format: 2, '$', '#'}}", 'price' => 50000000
|
assert_template_result "50$000$000#00", "{{ price | format: 2, '$', '#'}}", 'price' => 50000000
|
||||||
|
assert_template_result "-50$000$000#00", "{{ price | format: 2, '$', '#'}}", 'price' => -50000000
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_ceil
|
def test_ceil
|
||||||
|
|||||||
Reference in New Issue
Block a user