Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #156 from alup/master

Fix format output with custom currencies (4 decimal places)
  • Loading branch information...
commit 163062d74bd1dc4a8640964091e2c7c87e1cfdbe 2 parents c29746a + 2b13de9
@semmons99 semmons99 authored
View
1  AUTHORS
@@ -1,5 +1,6 @@
Abhay Kumar
Alex Speller
+Andreas Loupasakis
Andrew White
banjerluke
Bodaniel Jeanes
View
1  CHANGELOG.md
@@ -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
View
2  lib/money/money.rb
@@ -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::*]
View
10 lib/money/money/formatting.rb
@@ -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
@@ -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 << " "
View
39 spec/money/formatting_spec.rb
@@ -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") }
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.