Skip to content
Browse files

Add exponent to currency.

  • Loading branch information...
1 parent 6f4dcb8 commit 3147e7029c01e6c3cfdc004ed90072aba0d1a2d1 @ct-clearhaus ct-clearhaus committed Jan 2, 2013
Showing with 24 additions and 2 deletions.
  1. +1 −0 AUTHORS
  2. +1 −0 CHANGELOG.md
  3. +13 −1 lib/money/currency.rb
  4. +9 −1 spec/currency_spec.rb
View
1 AUTHORS
@@ -7,6 +7,7 @@ banjerluke
Bartosz Dz
Bodaniel Jeanes
bUg.
+Casper Thomsen
Choongmin Lee
Chris Kampmeier
Christian Billen
View
1 CHANGELOG.md
@@ -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
View
14 lib/money/currency.rb
@@ -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]
@@ -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+
@@ -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 ||= {
View
10 spec/currency_spec.rb
@@ -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
@@ -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

0 comments on commit 3147e70

Please sign in to comment.
Something went wrong with that request. Please try again.