Skip to content

Commit

Permalink
fix formatting for edge case with i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
cade committed Feb 25, 2016
1 parent 741d11e commit 9804a47
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/money/money/formatting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def format(*rules)
rules = default_formatting_rules.merge(rules)
rules = localize_formatting_rules(rules)
rules = translate_formatting_rules(rules) if rules[:translate]
escaped_decimal_mark = Regexp.escape(decimal_mark)

if fractional == 0
if rules[:display_free].respond_to?(:to_str)
Expand Down Expand Up @@ -236,9 +237,9 @@ def format(*rules)

# Inspiration: https://github.com/rails/rails/blob/16214d1108c31174c94503caced3855b0f6bad95/activesupport/lib/active_support/number_helper/number_to_rounded_converter.rb#L72-L79
if rules[:drop_trailing_zeros]
escaped_decimal_mark = Regexp.escape(decimal_mark)
formatted = formatted.sub(/(#{escaped_decimal_mark})(\d*[1-9])?0+\z/, '\1\2').sub(/#{escaped_decimal_mark}\z/, '')
end
has_decimal_value = !!(formatted =~ /#{escaped_decimal_mark}/)

thousands_separator_value = thousands_separator
# Determine thousands_separator
Expand Down Expand Up @@ -275,7 +276,7 @@ def format(*rules)
formatted="#{sign_before}#{sign}#{formatted}"
end

apply_decimal_mark_from_rules(formatted, rules)
apply_decimal_mark_from_rules(formatted, rules) if has_decimal_value

if rules[:with_currency]
formatted << " "
Expand Down
7 changes: 7 additions & 0 deletions spec/money/formatting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@
expect(money.format(:translate => true)).to eq("CAD$0.00")
end
end

context "with overridden i18n settings" do
it "should respect explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there’s no decimal component" do
expect(Money.new(300_000, 'ISK').format(:thousands_separator => ".", decimal_mark: ',')).to eq "kr300.000"
expect(Money.new(300_000, 'USD').format(:thousands_separator => ".", decimal_mark: ',', drop_trailing_zeros: true)).to eq "$3.000"
end
end
end

describe "#format" do
Expand Down

0 comments on commit 9804a47

Please sign in to comment.