Skip to content

Commit

Permalink
Formatting uses #decimal_mark on Money not Currency
Browse files Browse the repository at this point in the history
- Currency#decimal_mark is fixed for the currency and ignores i18n
  delimiter and separator of the application
  • Loading branch information
createdbypete committed Aug 29, 2015
1 parent 53ad18f commit cfd0a65
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@
return `nil` as `Comparable#==` will not rescue exceptions in the next release.
- Fix `Currency` specs for `#exponent` and `#decimal_places` not making assertions.
- Fix a couple of Ruby warnings found in specs.
- Use `Money#decimal_mark` when formatting with `rounded_infinite_precision` rule
set to `true`.

## 6.6.0
- Fixed VariableExchange#exchange_with for big numbers.
Expand Down
4 changes: 2 additions & 2 deletions lib/money/money/formatting.rb
Expand Up @@ -239,13 +239,13 @@ def format(*rules)
formatted = self.abs.to_s

if rules[:rounded_infinite_precision]
formatted.gsub!(/#{currency.decimal_mark}/, '.') unless '.' == currency.decimal_mark
formatted.gsub!(/#{decimal_mark}/, '.') unless '.' == decimal_mark
formatted = ((BigDecimal(formatted) * currency.subunit_to_unit).round / BigDecimal(currency.subunit_to_unit.to_s)).to_s("F")
formatted.gsub!(/\..*/) do |decimal_part|
decimal_part << '0' while decimal_part.length < (currency.decimal_places + 1)
decimal_part
end
formatted.gsub!(/\./, currency.decimal_mark) unless '.' == currency.decimal_mark
formatted.gsub!(/\./, decimal_mark) unless '.' == decimal_mark
end

sign = self.negative? ? '-' : ''
Expand Down
27 changes: 27 additions & 0 deletions spec/money/formatting_spec.rb
Expand Up @@ -569,6 +569,33 @@
expect(Money.new(BigDecimal.new('100012.5'), "EUR").format(:rounded_infinite_precision => true)).to eq "€1.000,13"
end
end

describe "with i18n = true" do
before do
Money.use_i18n = true
reset_i18n
I18n.locale = :de
I18n.backend.store_translations(
:de,
:number => { :currency => { :format => { :delimiter => ".", :separator => "," } } }

This comment has been minimized.

Copy link
@afuerhoff420

afuerhoff420 Mar 19, 2019

500.00

This comment has been minimized.

Copy link
@antstorm

antstorm Mar 30, 2019

Contributor

?

)
end

after do
reset_i18n
I18n.locale = :en
end

it 'does round fractional when set to true' do
expect(Money.new(BigDecimal.new('12.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$0,12"
expect(Money.new(BigDecimal.new('12.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$0,13"
expect(Money.new(BigDecimal.new('123.1'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0,123"
expect(Money.new(BigDecimal.new('123.5'), "BHD").format(:rounded_infinite_precision => true)).to eq "ب.د0,124"
expect(Money.new(BigDecimal.new('100.1'), "USD").format(:rounded_infinite_precision => true)).to eq "$1,00"
expect(Money.new(BigDecimal.new('109.5'), "USD").format(:rounded_infinite_precision => true)).to eq "$1,10"
expect(Money.new(BigDecimal.new('1'), "MGA").format(:rounded_infinite_precision => true)).to eq "Ar0,2"
end
end
end

context "when the monetary value is 0" do
Expand Down

0 comments on commit cfd0a65

Please sign in to comment.