Skip to content

Commit

Permalink
Merge pull request RubyMoney#237 from cathper/master
Browse files Browse the repository at this point in the history
Add exponent to currency
  • Loading branch information
semmons99 committed Jan 2, 2013
2 parents 6f4dcb8 + 3147e70 commit dd57780
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ banjerluke
Bartosz Dz
Bodaniel Jeanes
bUg.
Casper Thomsen
Choongmin Lee
Chris Kampmeier
Christian Billen
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Ensure BigDecimal.new always receives a string - compatibility fix for ruby-1.9.2-p320
- Update Maldivian Currency to MVR and fix ރ. to be ރ
- Add exponent to currency

## 5.1.0

Expand Down
14 changes: 13 additions & 1 deletion lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def stringify_keys
# @return [Integer]
attr_reader :subunit_to_unit

# The number of digits after the decimal separator.
#
# @return [Float]
attr_reader :exponent

# The decimal mark, or character used to separate the whole unit from the subunit.
#
# @return [String]
Expand Down Expand Up @@ -244,7 +249,7 @@ def hash
# @example
# Money::Currency.new(:usd) #=> #<Currency id: usd ...>
def inspect
"#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}>"
"#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, exponent: #{exponent}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}>"
end

# Returns a string representation corresponding to the upcase +id+
Expand Down Expand Up @@ -281,6 +286,13 @@ def symbol_first?
!!@symbol_first
end

# Returns the number of digits after the decimal separator.
#
# @return [Float]
def exponent
Math.log10(@subunit_to_unit)
end

# Cache decimal places for subunit_to_unit values. Common ones pre-cached.
def self.decimal_places_cache
@decimal_places_cache ||= {
Expand Down
10 changes: 9 additions & 1 deletion spec/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
describe "#inspect" do
it "works as documented" do
Money::Currency.new(:usd).inspect.should ==
%Q{#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, iso_code: USD, iso_numeric: 840, subunit: Cent>}
%Q{#<Money::Currency id: usd, priority: 1, symbol_first: true, thousands_separator: ,, html_entity: $, decimal_mark: ., name: United States Dollar, symbol: $, subunit_to_unit: 100, exponent: 2.0, iso_code: USD, iso_numeric: 840, subunit: Cent>}
end
end

Expand Down Expand Up @@ -121,6 +121,14 @@
end
end

describe "#exponent" do
it "conforms to iso 4217" do
Money::Currency.new(:jpy).exponent == 0
Money::Currency.new(:usd).exponent == 2
Money::Currency.new(:iqd).exponent == 3
end
end

describe "#decimal_places" do
it "proper places for known currency" do
Money::Currency.new(:mro).decimal_places == 1
Expand Down

0 comments on commit dd57780

Please sign in to comment.