Skip to content

Commit d386a4b

Browse files
authored
Merge pull request #1125 from jozr/main
Add method for cents-based check
2 parents b11fc67 + 9ebceb2 commit d386a4b

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
- Update thousands_separator for CHF
1414
- Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG)
1515

16+
## 6.20.0
17+
- Add `Currency#cents_based?` to check if currency is cents-based
18+
1619
## 6.19.0
1720

1821
- Change Peruvian Sol (PEN) decimal mark and thousands separator.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ including the currency symbol, name and much more.
113113

114114
``` ruby
115115
currency = Money.from_cents(1000, "USD").currency
116-
currency.iso_code #=> "USD"
117-
currency.name #=> "United States Dollar"
116+
currency.iso_code #=> "USD"
117+
currency.name #=> "United States Dollar"
118+
currency.cents_based? #=> true
118119
```
119120

120121
To define a new `Money::Currency` use `Money::Currency.register` as shown

lib/money/currency.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ def symbol_first?
403403
!!@symbol_first
404404
end
405405

406-
# Returns if a code currency is ISO.
406+
# Returns true if a code currency is ISO.
407407
#
408408
# @return [Boolean]
409409
#
@@ -414,6 +414,17 @@ def iso?
414414
iso_numeric && iso_numeric != ''
415415
end
416416

417+
# Returns true if a subunit is cents-based.
418+
#
419+
# @return [Boolean]
420+
#
421+
# @example
422+
# Money::Currency.new(:usd).cents_based?
423+
#
424+
def cents_based?
425+
subunit_to_unit == 100
426+
end
427+
417428
# Returns the relation between subunit and unit as a base 10 exponent.
418429
#
419430
# Note that MGA and MRU are exceptions and are rounded to 1

spec/currency_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,16 @@ def to_s
337337
end
338338
end
339339

340+
describe "#cents_based?" do
341+
it "returns true if the currency is cents-based" do
342+
expect(described_class.new(:mxn).cents_based?).to be true
343+
end
344+
345+
it "returns false if the currency is not cents-based" do
346+
expect(described_class.new(:clp).cents_based?).to be false
347+
end
348+
end
349+
340350
describe "#to_s" do
341351
it "works as documented" do
342352
expect(described_class.new(:usd).to_s).to eq("USD")

0 commit comments

Comments
 (0)