Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
#
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down