diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c425a6e5d..2206bba95b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - Fix typo in ILS currency - Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG) +## 6.20.0 +- Add `Currency#cents_based?` to check if currency is cents-based + ## 6.19.0 - Change Peruvian Sol (PEN) decimal mark and thousands separator. diff --git a/README.md b/README.md index b55438e3b4..fc740902f9 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,9 @@ including the currency symbol, name and much more. ``` ruby currency = Money.from_cents(1000, "USD").currency -currency.iso_code #=> "USD" -currency.name #=> "United States Dollar" +currency.iso_code #=> "USD" +currency.name #=> "United States Dollar" +currency.cents_based? #=> true ``` To define a new `Money::Currency` use `Money::Currency.register` as shown diff --git a/lib/money/currency.rb b/lib/money/currency.rb index b4e682b55b..cd604659ce 100644 --- a/lib/money/currency.rb +++ b/lib/money/currency.rb @@ -405,7 +405,7 @@ def symbol_first? !!@symbol_first end - # Returns if a code currency is ISO. + # Returns true if a code currency is ISO. # # @return [Boolean] # @@ -416,6 +416,17 @@ def iso? iso_numeric && iso_numeric != '' end + # Returns true if a subunit is cents-based. + # + # @return [Boolean] + # + # @example + # Money::Currency.new(:usd).cents_based? + # + def cents_based? + subunit_to_unit == 100 + end + # Returns the relation between subunit and unit as a base 10 exponent. # # Note that MGA and MRU are exceptions and are rounded to 1 diff --git a/spec/currency_spec.rb b/spec/currency_spec.rb index 2e01018bbc..c6fc192f4a 100644 --- a/spec/currency_spec.rb +++ b/spec/currency_spec.rb @@ -339,6 +339,16 @@ def to_s end end + describe "#cents_based?" do + it "returns true if the currency is cents-based" do + expect(described_class.new(:mxn).cents_based?).to be true + end + + it "returns false if the currency is not cents-based" do + expect(described_class.new(:clp).cents_based?).to be false + end + end + describe "#to_s" do it "works as documented" do expect(described_class.new(:usd).to_s).to eq("USD")