Skip to content
This repository has been archived by the owner on Apr 6, 2018. It is now read-only.

Commit

Permalink
Merge pull request RubyMoney#156 from alup/master
Browse files Browse the repository at this point in the history
Fix format output with custom currencies (4 decimal places)
  • Loading branch information
semmons99 committed Mar 19, 2012
2 parents c29746a + 2b13de9 commit 163062d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
@@ -1,5 +1,6 @@
Abhay Kumar
Alex Speller
Andreas Loupasakis
Andrew White
banjerluke
Bodaniel Jeanes
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -14,6 +14,7 @@
- Fix bug where rates exported to a file from VariableExchange leave the File
object open (GH-154)
- Added Money#positive? and Money#negative? methods (GH-157)
- Fix format function output for custom currencies (4 decimal places)

## 4.0.2

Expand Down
2 changes: 1 addition & 1 deletion lib/money/money.rb
Expand Up @@ -30,7 +30,7 @@ class Money
class << self
# Each Money object is associated to a bank object, which is responsible
# for currency exchange. This property allows you to specify the default
# bank object. The default value for this property is an instance if
# bank object. The default value for this property is an instance of
# +Bank::VariableExchange.+ It allows one to specify custom exchange rates.
#
# @return [Money::Bank::*]
Expand Down
10 changes: 8 additions & 2 deletions lib/money/money/formatting.rb
Expand Up @@ -169,7 +169,7 @@ def format(*rules)
end

formatted = rules[:no_cents] ? "#{self.to_s.to_i}" : self.to_s

if rules[:no_cents_if_whole] && cents % currency.subunit_to_unit == 0
formatted = "#{self.to_s.to_i}"
end
Expand Down Expand Up @@ -204,7 +204,13 @@ def format(*rules)
end

# Apply thousands_separator
formatted.gsub!(/(\d)(?=(?:\d{3})+(?:[^\d]|$))/, "\\1#{thousands_separator_value}")
regexp_decimal = Regexp.escape(decimal_mark)
regexp_format = if formatted =~ /#{regexp_decimal}/
/(\d)(?=(?:\d{3})+(?:#{regexp_decimal}))/
else
/(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
end
formatted.gsub!(regexp_format, "\\1#{thousands_separator_value}")

if rules[:with_currency]
formatted << " "
Expand Down
39 changes: 39 additions & 0 deletions spec/money/formatting_spec.rb
Expand Up @@ -4,6 +4,9 @@

describe Money, "formatting" do

BAR = '{ "priority": 1, "iso_code": "BAR", "iso_numeric": "840", "name": "Dollar with 4 decimal places", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": "," }'
EU4 = '{ "priority": 1, "iso_code": "EU4", "iso_numeric": "841", "name": "Euro with 4 decimal places", "symbol": "€", "subunit": "Cent", "subunit_to_unit": 10000, "symbol_first": true, "html_entity": "€", "decimal_mark": ",", "thousands_separator": "." }'

context "without i18n" do
subject { Money.empty("USD") }

Expand Down Expand Up @@ -403,5 +406,41 @@
end
end

context "custom currencies with 4 decimal places" do
before :each do
Money::Currency.register(JSON.parse(BAR, :symbolize_names => true))
Money::Currency.register(JSON.parse(EU4, :symbolize_names => true))
end

it "respects custom subunit to unit, decimal and thousands separator" do
Money.new(4, "BAR").format.should == "$0.0004"
Money.new(4, "EU4").format.should == "€0,0004"

Money.new(24, "BAR").format.should == "$0.0024"
Money.new(24, "EU4").format.should == "€0,0024"

Money.new(324, "BAR").format.should == "$0.0324"
Money.new(324, "EU4").format.should == "€0,0324"

Money.new(5324, "BAR").format.should == "$0.5324"
Money.new(5324, "EU4").format.should == "€0,5324"

Money.new(65324, "BAR").format.should == "$6.5324"
Money.new(65324, "EU4").format.should == "€6,5324"

Money.new(865324, "BAR").format.should == "$86.5324"
Money.new(865324, "EU4").format.should == "€86,5324"

Money.new(1865324, "BAR").format.should == "$186.5324"
Money.new(1865324, "EU4").format.should == "€186,5324"

Money.new(33310034, "BAR").format.should == "$3,331.0034"
Money.new(33310034, "EU4").format.should == "€3.331,0034"

Money.new(88833310034, "BAR").format.should == "$8,883,331.0034"
Money.new(88833310034, "EU4").format.should == "€8.883.331,0034"
end

end
end

0 comments on commit 163062d

Please sign in to comment.