money 6.19.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +97 -47
- data/LICENSE +1 -1
- data/README.md +51 -54
- data/config/currency_backwards_compatible.json +1 -0
- data/config/currency_iso.json +42 -12
- data/config/currency_non_iso.json +1 -1
- data/lib/money/bank/base.rb +2 -0
- data/lib/money/bank/single_currency.rb +2 -0
- data/lib/money/bank/variable_exchange.rb +2 -0
- data/lib/money/currency/heuristics.rb +1 -1
- data/lib/money/currency/loader.rb +2 -0
- data/lib/money/currency.rb +35 -9
- data/lib/money/locale_backend/base.rb +2 -0
- data/lib/money/locale_backend/currency.rb +2 -0
- data/lib/money/locale_backend/errors.rb +2 -0
- data/lib/money/locale_backend/i18n.rb +4 -1
- data/lib/money/money/allocation.rb +27 -8
- data/lib/money/money/arithmetic.rb +31 -13
- data/lib/money/money/constructors.rb +2 -0
- data/lib/money/money/formatter.rb +13 -62
- data/lib/money/money/formatting_rules.rb +9 -59
- data/lib/money/money/locale_backend.rb +1 -3
- data/lib/money/money.rb +105 -119
- data/lib/money/rates_store/memory.rb +2 -0
- data/lib/money/version.rb +3 -1
- data/lib/money.rb +3 -0
- data/money.gemspec +8 -3
- metadata +22 -17
- data/lib/money/locale_backend/legacy.rb +0 -28
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 435fd498ba0ce70e5d0ee2079e0e99d93abc2c17440f7a6cacda1176641b7d3a
|
|
4
|
+
data.tar.gz: 81fdc617e20b27d8628ba2f4859fa2fd41a9cbc04c3a44ddf689036b4414f40b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e2ef4767b6d9a50fa7d6eed46570249af8c2e2bade1c8b11aee93d737d1a8c484d74f1aaf837a880e28af8be44b1c5413a5bea854a015b936026032a3070dcd
|
|
7
|
+
data.tar.gz: 756de612cdebdb1e4cf3311f460a3f5118fd775a4f1760ad211c4fd6816804cc16f8b4c461bb8a0292031d7b03a22ae0f291844467791fbbc1a9f1bc46519060
|
data/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,61 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 7.0.0
|
|
6
|
+
|
|
7
|
+
- **Breaking change**: Require Ruby >= 3.1 and i18n ~> 1.9
|
|
8
|
+
- **Breaking change**: Remove deprecated formatting rules:
|
|
9
|
+
- `:html`
|
|
10
|
+
- `:html_wrap_symbol`
|
|
11
|
+
- `:symbol_position`
|
|
12
|
+
- `:symbol_before_without_space`
|
|
13
|
+
- `:symbol_after_without_space`
|
|
14
|
+
- **Breaking change**: Remove deprecated methods:
|
|
15
|
+
- `Money.infinite_precision`.
|
|
16
|
+
- `Money.infinite_precision=`.
|
|
17
|
+
- `Money#currency_as_string`.
|
|
18
|
+
- `Money#currency_as_string=`.
|
|
19
|
+
- `Money.use_i18n` and `Money.use_i18n=` (use `Money.locale_backend = :i18n` instead).
|
|
20
|
+
- **Breaking change**: Remove legacy locale backend. The default locale backend is now `:currency` which uses currency definitions for formatting.
|
|
21
|
+
- **Breaking change**: Default currency is now `nil` instead of `USD`. If you want to keep the previous behavior, set `Money.default_currency = Money::Currency.new("USD")` in your initializer. Initializing a Money object will raise a `Currency::NoCurrency` if no currency is set.
|
|
22
|
+
- **Breaking change**: The default rounding mode has changed from `BigDecimal::ROUND_HALF_EVEN` to `BigDecimal::ROUND_HALF_UP`. Set it explicitly using `Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN` to keep the previous behavior.
|
|
23
|
+
- **Potential breaking change**: Fix RSD (Serbian Dinar) formatting to be like `12.345,42 RSD`
|
|
24
|
+
- **Potential breaking change**: Fix USDC decimals places from 2 to 6
|
|
25
|
+
- **Potential breaking change**: Fix MGA (Malagasy Ariary) to be a zero-decimal currency (changing subunit_to_unit from 5 to 1)
|
|
26
|
+
- **Potential breaking change**: Remove special handling for Japanese language only
|
|
27
|
+
- **Potential breaking change**: Adjust formatting rules to use i18n translations for `:format`
|
|
28
|
+
- **Potential breaking change**: Not allow divide by zero
|
|
29
|
+
- Updated Armenian Dram sign and HTML entity
|
|
30
|
+
- Updated the Turkmen Manat symbol and HTML entity and added disambiguation symbol for TMM
|
|
31
|
+
- Expose Money::VERSION
|
|
32
|
+
- Fix typo in ILS currency
|
|
33
|
+
- Add Zimbabwe Gold (ZWG) currency
|
|
34
|
+
- Update thousands_separator for CHF
|
|
35
|
+
- Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG)
|
|
36
|
+
- Add `Money.strict_eql_compare = true` so that comparing zero amounts with different currencies using `Money#eql?` returns `false`
|
|
37
|
+
```rb
|
|
38
|
+
Money.new(0, "USD").eql?(Money.new(0, "EUR")) #=> true
|
|
39
|
+
#> [DEPRECATION] Comparing 0 USD with 0 EUR using `#eql?` will return false…
|
|
40
|
+
|
|
41
|
+
Money.strict_eql_compare = true
|
|
42
|
+
Money.new(0, "USD").eql?(Money.new(0, "EUR")) #=> false
|
|
43
|
+
```
|
|
44
|
+
- Add `Money#to_nearest_cash_value` to return a rounded Money instance to the smallest denomination
|
|
45
|
+
- Deprecate `Money#round_to_nearest_cash_value` in favor of calling `to_nearest_cash_value.fractional`
|
|
46
|
+
- Deprecate `Money#dollars` in favor of `Money#amount`.
|
|
47
|
+
- Deprecate `Money.from_dollars` in favor of `Money.from_amount`.
|
|
48
|
+
- Add `Money::Currency#cents_based?` to check if currency is cents-based
|
|
49
|
+
- Add ability to nest `Money.with_rounding_mode` blocks
|
|
50
|
+
- Allow `nil` to be used as a default_currency
|
|
51
|
+
- Improve performance of `find_by_iso_numeric` method
|
|
52
|
+
- Add ability to nest `Money.with_bank` blocks
|
|
53
|
+
- Refactor `Money::Allocation.generate` to support configurable per-split precision by accepting an Integer as a second argument, fixing allocation failures with large arrays and infinite decimal expansions.
|
|
6
54
|
|
|
7
55
|
## 6.19.0
|
|
8
56
|
|
|
9
57
|
- Change Peruvian Sol (PEN) decimal mark and thousands separator.
|
|
58
|
+
- Fix deprecation warning for BigDecimal being moved out from stdlib.
|
|
10
59
|
- Improves Precision and Simplifies Allocation Logic
|
|
11
60
|
- explicit declaration of i18n locales
|
|
12
61
|
- Add space to CHF format
|
|
@@ -15,6 +64,7 @@
|
|
|
15
64
|
## 6.18.0
|
|
16
65
|
|
|
17
66
|
- Add second dobra (STN) from São Tomé and Príncipe
|
|
67
|
+
- Add Sierra Leonean (new) leone (SLE) from Sierra Leone
|
|
18
68
|
- Correct South African Rand (ZAR) to use comma decimal mark, and space thousands separator
|
|
19
69
|
- Use euro symbol as html_entity for euro currency
|
|
20
70
|
- Update Georgian Lari symbol
|
|
@@ -211,7 +261,7 @@
|
|
|
211
261
|
return `nil` as `Comparable#==` will not rescue exceptions in the next release.
|
|
212
262
|
- Fix `Currency` specs for `#exponent` and `#decimal_places` not making assertions.
|
|
213
263
|
- Fix a couple of Ruby warnings found in specs.
|
|
214
|
-
- Fix `Money#-`,`Money#+`
|
|
264
|
+
- Fix `Money#-`,`Money#+` arithmetic for Ruby 2.3+ : check for zero value without using eql? with a Fixnum. (#577)
|
|
215
265
|
- Use `Money#decimal_mark` when formatting with `rounded_infinite_precision` rule
|
|
216
266
|
set to `true`.
|
|
217
267
|
- Replaced meta-defined `thousands_separator` and `decimal_mark` methods with regular methods. (#579)
|
|
@@ -258,7 +308,7 @@
|
|
|
258
308
|
- Fix formatting of NGN - show symbol before amount
|
|
259
309
|
- Switch default and alternate symbols for RUB currency
|
|
260
310
|
- Fix symbol for TRY currency
|
|
261
|
-
- Add `Money.default_formatting_rules` hash, meant to define default rules for
|
|
311
|
+
- Add `Money.default_formatting_rules` hash, meant to define default rules for every time `Money#format` is called. They can be overwritten if provided on method call
|
|
262
312
|
- Add support for the new official symbol for Russian Ruble (RUB) — «₽»
|
|
263
313
|
|
|
264
314
|
## 6.2.1
|
|
@@ -427,7 +477,7 @@ Features
|
|
|
427
477
|
|
|
428
478
|
Bugfixes
|
|
429
479
|
--------
|
|
430
|
-
- EEK currency is no longer used, kept for BC ([#issue/110](
|
|
480
|
+
- EEK currency is no longer used, kept for BC ([#issue/110](https://github.com/RubyMoney/money/issues/110))
|
|
431
481
|
- Lithuanian Litas symbol position fixed (laurynas)
|
|
432
482
|
- Fixed README typos (phlipper)
|
|
433
483
|
- Fixed README typos (pwim)
|
|
@@ -553,11 +603,11 @@ Bugfixes
|
|
|
553
603
|
--------
|
|
554
604
|
- Updated Money#to_s to respect :separator and :subunit_to_unit.
|
|
555
605
|
- Fixed Money#format for :subunit_to_unit != 100.
|
|
556
|
-
([#issue/37](
|
|
606
|
+
([#issue/37](https://github.com/RubyMoney/money/issues/37))
|
|
557
607
|
- Fixed String#to_money for :subunit_to_unit != 100.
|
|
558
|
-
([#issue/36](
|
|
608
|
+
([#issue/36](https://github.com/RubyMoney/money/issues/36))
|
|
559
609
|
- Removed duplicate currencies.
|
|
560
|
-
([#issue/38](
|
|
610
|
+
([#issue/38](https://github.com/RubyMoney/money/issues/38))
|
|
561
611
|
- Fixed issue related to JRuby returning 2 for Math.log10(1000).floor instead
|
|
562
612
|
of correctly returning 3.
|
|
563
613
|
|
|
@@ -568,22 +618,22 @@ Features
|
|
|
568
618
|
--------
|
|
569
619
|
- Added support for creating objects with the main monetary unit instead of
|
|
570
620
|
cents.
|
|
571
|
-
([#issue/25](
|
|
621
|
+
([#issue/25](https://github.com/RubyMoney/money/issues/25))
|
|
572
622
|
- Deprecated `Money#format` with separate params instead of Hash. Deprecation
|
|
573
623
|
target set to Money 3.5.0.
|
|
574
|
-
([#issue/31](
|
|
624
|
+
([#issue/31](https://github.com/RubyMoney/money/issues/31))
|
|
575
625
|
- Deprecated `Money#new(0, currency: "EUR")` in favor of
|
|
576
626
|
`Money#new(0, "EUR")`. Deprecation target set to Money 3.5.0.
|
|
577
|
-
([#issue/31](
|
|
627
|
+
([#issue/31](https://github.com/RubyMoney/money/issues/31))
|
|
578
628
|
- Throw ArgumentError when trying to multiply two Money objects together.
|
|
579
|
-
([#issue/29](
|
|
629
|
+
([#issue/29](https://github.com/RubyMoney/money/issues/29))
|
|
580
630
|
- Update Money#parse to use :subunit_to_unit
|
|
581
|
-
([#issue/30](
|
|
631
|
+
([#issue/30](https://github.com/RubyMoney/money/issues/30))
|
|
582
632
|
|
|
583
633
|
Bugfixes
|
|
584
634
|
--------
|
|
585
635
|
- Downgraded required_rubygems_version to >= 1.3.6.
|
|
586
|
-
([#issue/26](
|
|
636
|
+
([#issue/26](https://github.com/RubyMoney/money/issues/26))
|
|
587
637
|
- Use BigDecimal when floating point calculations are needed.
|
|
588
638
|
- Ruby 1.9.2 compatibility enhancements.
|
|
589
639
|
|
|
@@ -593,32 +643,32 @@ Money 3.1.0
|
|
|
593
643
|
Features
|
|
594
644
|
--------
|
|
595
645
|
- Implemented `Money::Bank::Base`.
|
|
596
|
-
([#issue/14](
|
|
646
|
+
([#issue/14](https://github.com/RubyMoney/money/issues/14))
|
|
597
647
|
- Added `Money::Bank::Base#exchange_with`.
|
|
598
648
|
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
|
599
649
|
3.2.0.
|
|
600
|
-
-
|
|
650
|
+
- Implemented `Money::Bank::VariableExchange`
|
|
601
651
|
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
|
602
652
|
3.2.0.
|
|
603
653
|
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
|
604
654
|
Deprecation target set to Money 3.2.0.
|
|
605
|
-
([#issue/16](
|
|
655
|
+
([#issue/16](https://github.com/RubyMoney/money/issues/16))
|
|
606
656
|
- Implemented `#has` for `Money` and `Money::Currency`.
|
|
607
657
|
- Refactored test suite to conform to RSpec conventions.
|
|
608
|
-
- Moved project from [FooBarWidget](
|
|
609
|
-
[RubyMoney](
|
|
658
|
+
- Moved project from [FooBarWidget](https://github.com/FooBarWidget) to
|
|
659
|
+
[RubyMoney](https://github.com/RubyMoney)
|
|
610
660
|
- Added Simone Carletti to list of authors.
|
|
611
661
|
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
|
612
662
|
`Money::Bank::Base`.
|
|
613
|
-
([#issue/18](
|
|
663
|
+
([#issue/18](https://github.com/RubyMoney/money/issues/18))
|
|
614
664
|
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
|
615
|
-
([#issue/19](
|
|
616
|
-
- Added [google_currency](
|
|
665
|
+
([#issue/19](https://github.com/RubyMoney/money/issues/19))
|
|
666
|
+
- Added [google_currency](https://github.com/RubyMoney/google_currency) to list
|
|
617
667
|
of Currency Exchange Implementations.
|
|
618
668
|
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
|
619
|
-
([#issue/21](
|
|
669
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
620
670
|
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
|
621
|
-
([#issue/21](
|
|
671
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
622
672
|
- Removed dependency on Jeweler.
|
|
623
673
|
- Replaced usage of hanna with yardoc.
|
|
624
674
|
- Rewrote/reformatted all documentation.
|
|
@@ -626,30 +676,30 @@ Features
|
|
|
626
676
|
Bugfixes
|
|
627
677
|
--------
|
|
628
678
|
- Fixed incorrect URLs in documentation.
|
|
629
|
-
([#issue/17](
|
|
679
|
+
([#issue/17](https://github.com/RubyMoney/money/issues/17))
|
|
630
680
|
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
|
631
|
-
([#issue/20](
|
|
681
|
+
([#issue/20](https://github.com/RubyMoney/money/issues/20))
|
|
632
682
|
- Updated Ghanaian Cedi to use correct ISO Code, GHS.
|
|
633
|
-
([#issue/22](
|
|
683
|
+
([#issue/22](https://github.com/RubyMoney/money/issues/22))
|
|
634
684
|
- Make `default` rake task call `spec`.
|
|
635
|
-
([#issue/23](
|
|
685
|
+
([#issue/23](https://github.com/RubyMoney/money/issues/23))
|
|
636
686
|
|
|
637
687
|
Money 3.1.0.pre3
|
|
638
688
|
================
|
|
639
689
|
|
|
640
690
|
Features
|
|
641
691
|
--------
|
|
642
|
-
- Added [google_currency](
|
|
692
|
+
- Added [google_currency](https://github.com/RubyMoney/google_currency) to list
|
|
643
693
|
of Currency Exchange Implementations.
|
|
644
694
|
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
|
645
|
-
([#issue/21](
|
|
695
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
646
696
|
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
|
647
|
-
([#issue/21](
|
|
697
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
648
698
|
|
|
649
699
|
Bugfixes
|
|
650
700
|
--------
|
|
651
701
|
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
|
652
|
-
([#issue/20](
|
|
702
|
+
([#issue/20](https://github.com/RubyMoney/money/issues/20))
|
|
653
703
|
|
|
654
704
|
Money 3.1.0.pre2
|
|
655
705
|
================
|
|
@@ -658,14 +708,14 @@ Features
|
|
|
658
708
|
--------
|
|
659
709
|
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
|
660
710
|
`Money::Bank::Base`.
|
|
661
|
-
([#issue/18](
|
|
711
|
+
([#issue/18](https://github.com/RubyMoney/money/issues/18))
|
|
662
712
|
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
|
663
|
-
([#issue/19](
|
|
713
|
+
([#issue/19](https://github.com/RubyMoney/money/issues/19))
|
|
664
714
|
|
|
665
715
|
Bugfixes
|
|
666
716
|
--------
|
|
667
717
|
- Fixed incorrect URLs in documentation.
|
|
668
|
-
([#issue/17](
|
|
718
|
+
([#issue/17](https://github.com/RubyMoney/money/issues/17))
|
|
669
719
|
|
|
670
720
|
Money 3.1.0.pre1
|
|
671
721
|
================
|
|
@@ -673,26 +723,26 @@ Money 3.1.0.pre1
|
|
|
673
723
|
Features
|
|
674
724
|
--------
|
|
675
725
|
- Implemented `Money::Bank::Base`.
|
|
676
|
-
([#issue/14](
|
|
726
|
+
([#issue/14](https://github.com/RubyMoney/money/issues/14))
|
|
677
727
|
- Added `Money::Bank::Base#exchange_with`.
|
|
678
728
|
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
|
679
729
|
3.2.0.
|
|
680
|
-
-
|
|
730
|
+
- Implemented `Money::Bank::VariableExchange`
|
|
681
731
|
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
|
682
732
|
3.2.0.
|
|
683
733
|
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
|
684
734
|
Deprecation target set to Money 3.2.0.
|
|
685
|
-
([#issue/16](
|
|
735
|
+
([#issue/16](https://github.com/RubyMoney/money/issues/16))
|
|
686
736
|
- Implemented `#has` for `Money` and `Money::Currency`.
|
|
687
737
|
- Refactored test suite to conform to RSpec conventions.
|
|
688
|
-
- Moved project from [FooBarWidget](
|
|
689
|
-
[RubyMoney](
|
|
738
|
+
- Moved project from [FooBarWidget](https://github.com/FooBarWidget) to
|
|
739
|
+
[RubyMoney](https://github.com/RubyMoney)
|
|
690
740
|
- Added Simone Carletti to list of authors.
|
|
691
741
|
|
|
692
742
|
Bugfixes
|
|
693
743
|
--------
|
|
694
744
|
- Fixed rounding error in `Numeric#to_money`.
|
|
695
|
-
([#issue/15](
|
|
745
|
+
([#issue/15](https://github.com/RubyMoney/money/issues/15))
|
|
696
746
|
|
|
697
747
|
Money 3.0.5
|
|
698
748
|
===========
|
|
@@ -703,11 +753,11 @@ Features
|
|
|
703
753
|
- Added ability to pass a block to `VariableExchangeBank#new` or `#exchange`,
|
|
704
754
|
specifying a custom truncation method
|
|
705
755
|
- Added optional `currency` argument to` Numeric#to_money`.
|
|
706
|
-
([#issue/11](
|
|
756
|
+
([#issue/11](https://github.com/RubyMoney/money/issues/11))
|
|
707
757
|
- Added optional `currency` argument to `String#to_money`.
|
|
708
|
-
([#issue/11](
|
|
758
|
+
([#issue/11](https://github.com/RubyMoney/money/issues/11))
|
|
709
759
|
- Use '¤' as the default currency symbol.
|
|
710
|
-
([#issue/10](
|
|
760
|
+
([#issue/10](https://github.com/RubyMoney/money/issues/10))
|
|
711
761
|
|
|
712
762
|
Bugfixes
|
|
713
763
|
--------
|
|
@@ -716,7 +766,7 @@ Bugfixes
|
|
|
716
766
|
- Fixed issue when exchanging currencies with different `:subunit_to_unit`
|
|
717
767
|
values.
|
|
718
768
|
- `Numeric#to_money` now respects `:subunit_to_unit`.
|
|
719
|
-
([#issue/12](
|
|
769
|
+
([#issue/12](https://github.com/RubyMoney/money/issues/12))
|
|
720
770
|
|
|
721
771
|
Money 3.0.4
|
|
722
772
|
===========
|
|
@@ -763,7 +813,7 @@ Features
|
|
|
763
813
|
--------
|
|
764
814
|
- Version Bump due to compatibility changes with ActiveRecord. See
|
|
765
815
|
conversation
|
|
766
|
-
[here](
|
|
816
|
+
[here](https://github.com/RubyMoney/money/issues/4#issuecomment-224880)
|
|
767
817
|
for more information.
|
|
768
818
|
|
|
769
819
|
Money 2.3.0
|
data/LICENSE
CHANGED
|
@@ -2,7 +2,7 @@ MIT License
|
|
|
2
2
|
|
|
3
3
|
Copyright (c) 2005 Tobias Lutke
|
|
4
4
|
Copyright (c) 2008 Phusion
|
|
5
|
-
Copyright (c)
|
|
5
|
+
Copyright (c) 2025 Shane Emmons
|
|
6
6
|
|
|
7
7
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
8
|
of this software and associated documentation files (the "Software"), to deal
|
data/README.md
CHANGED
|
@@ -2,17 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://rubygems.org/gems/money)
|
|
4
4
|
[](https://github.com/RubyMoney/money/actions/workflows/ruby.yml)
|
|
5
|
-
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://rubymoney.github.io/money/)
|
|
6
|
+
[](https://opensource.org/license/MIT)
|
|
8
7
|
|
|
9
|
-
:warning: Please read the [
|
|
8
|
+
:warning: Please read the [upgrade guides](#upgrade-guides) before upgrading to a new major version.
|
|
10
9
|
|
|
11
10
|
If you miss String parsing, check out the new [monetize gem](https://github.com/RubyMoney/monetize).
|
|
12
11
|
|
|
13
12
|
## Contributing
|
|
14
13
|
|
|
15
|
-
See the [Contribution Guidelines](https://github.com/RubyMoney/money/blob/
|
|
14
|
+
See the [Contribution Guidelines](https://github.com/RubyMoney/money/blob/main/CONTRIBUTING.md)
|
|
16
15
|
|
|
17
16
|
## Introduction
|
|
18
17
|
|
|
@@ -33,15 +32,13 @@ A Ruby Library for dealing with money and currency conversion.
|
|
|
33
32
|
### Resources
|
|
34
33
|
|
|
35
34
|
- [Website](https://rubymoney.github.io/money/)
|
|
36
|
-
- [API Documentation](
|
|
35
|
+
- [API Documentation](https://www.rubydoc.info/gems/money/frames)
|
|
37
36
|
- [Git Repository](https://github.com/RubyMoney/money)
|
|
38
37
|
|
|
39
38
|
### Notes
|
|
40
39
|
|
|
41
40
|
- Your app must use UTF-8 to function with this library. There are a
|
|
42
41
|
number of non-ASCII currency attributes.
|
|
43
|
-
- This app requires JSON. If you're using JRuby < 1.7.0
|
|
44
|
-
you'll need to add `gem "json"` to your Gemfile or similar.
|
|
45
42
|
|
|
46
43
|
## Downloading
|
|
47
44
|
|
|
@@ -57,7 +54,7 @@ The development version (hosted on Github) can be installed with:
|
|
|
57
54
|
|
|
58
55
|
## Usage
|
|
59
56
|
|
|
60
|
-
```
|
|
57
|
+
```ruby
|
|
61
58
|
require 'money'
|
|
62
59
|
|
|
63
60
|
# explicitly define locales
|
|
@@ -105,7 +102,7 @@ Currencies are consistently represented as instances of `Money::Currency`.
|
|
|
105
102
|
The most part of `Money` APIs allows you to supply either a `String` or a
|
|
106
103
|
`Money::Currency`.
|
|
107
104
|
|
|
108
|
-
```
|
|
105
|
+
```ruby
|
|
109
106
|
Money.from_cents(1000, "USD") == Money.from_cents(1000, Money::Currency.new("USD"))
|
|
110
107
|
Money.from_cents(1000, "EUR").currency == Money::Currency.new("EUR")
|
|
111
108
|
```
|
|
@@ -113,16 +110,17 @@ Money.from_cents(1000, "EUR").currency == Money::Currency.new("EUR")
|
|
|
113
110
|
A `Money::Currency` instance holds all the information about the currency,
|
|
114
111
|
including the currency symbol, name and much more.
|
|
115
112
|
|
|
116
|
-
```
|
|
113
|
+
```ruby
|
|
117
114
|
currency = Money.from_cents(1000, "USD").currency
|
|
118
|
-
currency.iso_code
|
|
119
|
-
currency.name
|
|
115
|
+
currency.iso_code #=> "USD"
|
|
116
|
+
currency.name #=> "United States Dollar"
|
|
117
|
+
currency.cents_based? #=> true
|
|
120
118
|
```
|
|
121
119
|
|
|
122
120
|
To define a new `Money::Currency` use `Money::Currency.register` as shown
|
|
123
121
|
below.
|
|
124
122
|
|
|
125
|
-
```
|
|
123
|
+
```ruby
|
|
126
124
|
curr = {
|
|
127
125
|
priority: 1,
|
|
128
126
|
iso_code: "USD",
|
|
@@ -166,7 +164,7 @@ selector like the one available
|
|
|
166
164
|
custom methods to return the list of major currencies and all currencies as
|
|
167
165
|
follows:
|
|
168
166
|
|
|
169
|
-
```
|
|
167
|
+
```ruby
|
|
170
168
|
# Returns an array of currency id where
|
|
171
169
|
# priority < 10
|
|
172
170
|
def major_currencies(hash)
|
|
@@ -194,23 +192,22 @@ all_currencies(Money::Currency.table)
|
|
|
194
192
|
|
|
195
193
|
### Default Currency
|
|
196
194
|
|
|
197
|
-
|
|
198
|
-
using:
|
|
195
|
+
A default currency is not set by default. If a default currency is not set, it will raise an error when you try to initialize a `Money` object without explicitly passing a currency or parse a string that does not contain a currency. You can set a default currency for your application by using:
|
|
199
196
|
|
|
200
|
-
```
|
|
197
|
+
```ruby
|
|
201
198
|
Money.default_currency = Money::Currency.new("CAD")
|
|
202
199
|
```
|
|
203
200
|
|
|
204
|
-
If you use [Rails](https://github.com/RubyMoney/money/tree/
|
|
201
|
+
If you use [Rails](https://github.com/RubyMoney/money/tree/main#ruby-on-rails), then `config/initializers/money.rb` is a very good place to put this.
|
|
205
202
|
|
|
206
203
|
### Currency Exponent
|
|
207
204
|
|
|
208
205
|
The exponent of a money value is the number of digits after the decimal
|
|
209
206
|
separator (which separates the major unit from the minor unit). See e.g.
|
|
210
|
-
[ISO 4217](https://www.
|
|
207
|
+
[ISO 4217](https://www.iso.org/iso-4217-currency-codes.html) for more
|
|
211
208
|
information. You can find the exponent (as an `Integer`) by
|
|
212
209
|
|
|
213
|
-
```
|
|
210
|
+
```ruby
|
|
214
211
|
Money::Currency.new("USD").exponent # => 2
|
|
215
212
|
Money::Currency.new("JPY").exponent # => 0
|
|
216
213
|
Money::Currency.new("MGA").exponent # => 1
|
|
@@ -220,7 +217,7 @@ Money::Currency.new("MGA").exponent # => 1
|
|
|
220
217
|
|
|
221
218
|
To find a given currency by ISO 4217 numeric code (three digits) you can do
|
|
222
219
|
|
|
223
|
-
```
|
|
220
|
+
```ruby
|
|
224
221
|
Money::Currency.find_by_iso_numeric(978) #=> Money::Currency.new(:eur)
|
|
225
222
|
```
|
|
226
223
|
|
|
@@ -230,7 +227,7 @@ Exchanging money is performed through an exchange bank object. The default
|
|
|
230
227
|
exchange bank object requires one to manually specify the exchange rate. Here's
|
|
231
228
|
an example of how it works:
|
|
232
229
|
|
|
233
|
-
```
|
|
230
|
+
```ruby
|
|
234
231
|
Money.add_rate("USD", "CAD", 1.24515)
|
|
235
232
|
Money.add_rate("CAD", "USD", 0.803115)
|
|
236
233
|
|
|
@@ -240,7 +237,7 @@ Money.ca_dollar(100).exchange_to("USD") # => Money.from_cents(80, "USD")
|
|
|
240
237
|
|
|
241
238
|
Comparison and arithmetic operations work as expected:
|
|
242
239
|
|
|
243
|
-
```
|
|
240
|
+
```ruby
|
|
244
241
|
Money.from_cents(1000, "USD") <=> Money.from_cents(900, "USD") # => 1; 9.00 USD is smaller
|
|
245
242
|
Money.from_cents(1000, "EUR") + Money.from_cents(10, "EUR") == Money.from_cents(1010, "EUR")
|
|
246
243
|
|
|
@@ -348,14 +345,13 @@ end
|
|
|
348
345
|
The following example implements a `Redis` store to save exchange rates to a redis database.
|
|
349
346
|
|
|
350
347
|
```ruby
|
|
351
|
-
|
|
352
348
|
class RedisRateStore
|
|
353
349
|
INDEX_KEY_SEPARATOR = '_TO_'.freeze
|
|
354
|
-
|
|
350
|
+
|
|
355
351
|
# Using second db of the redis instance
|
|
356
352
|
# because sidekiq uses the first db
|
|
357
353
|
REDIS_DATABASE = 1
|
|
358
|
-
|
|
354
|
+
|
|
359
355
|
# Using Hash to store rates data
|
|
360
356
|
REDIS_STORE_KEY = 'rates'
|
|
361
357
|
|
|
@@ -410,9 +406,9 @@ Money.from_cents(1000, 'USD').exchange_to('CAD') #=> #<Money fractional:900 curr
|
|
|
410
406
|
```
|
|
411
407
|
|
|
412
408
|
There is nothing stopping you from creating store objects which scrapes
|
|
413
|
-
[XE](
|
|
409
|
+
[XE](https://www.xe.com) for the current rates or just returns `rand(2)`:
|
|
414
410
|
|
|
415
|
-
```
|
|
411
|
+
```ruby
|
|
416
412
|
Money.default_bank = Money::Bank::VariableExchange.new(StoreWhichScrapesXeDotCom.new)
|
|
417
413
|
```
|
|
418
414
|
|
|
@@ -426,7 +422,7 @@ Money.default_bank = MyCustomBank.new(Money::RatesStore::Memory.new)
|
|
|
426
422
|
If you wish to disable automatic currency conversion to prevent arithmetic when
|
|
427
423
|
currencies don't match:
|
|
428
424
|
|
|
429
|
-
```
|
|
425
|
+
```ruby
|
|
430
426
|
Money.disallow_currency_conversion!
|
|
431
427
|
```
|
|
432
428
|
|
|
@@ -448,9 +444,9 @@ implementations.
|
|
|
448
444
|
|
|
449
445
|
## Formatting
|
|
450
446
|
|
|
451
|
-
There are several formatting rules for when `Money#format` is called. For more information, check out the [formatting module source](https://github.com/RubyMoney/money/blob/
|
|
447
|
+
There are several formatting rules for when `Money#format` is called. For more information, check out the [formatting module source](https://github.com/RubyMoney/money/blob/main/lib/money/money/formatter.rb), or read the latest release's [rdoc version](https://www.rubydoc.info/gems/money/Money/Formatter).
|
|
452
448
|
|
|
453
|
-
If you wish to format money according to the EU's [Rules for expressing monetary units](
|
|
449
|
+
If you wish to format money according to the EU's [Rules for expressing monetary units](https://style-guide.europa.eu/en/content/-/isg/topic?identifier=7.3.3-rules-for-expressing-monetary-units#id370303__id370303_PositionISO) in either English, Irish, Latvian or Maltese:
|
|
454
450
|
|
|
455
451
|
```ruby
|
|
456
452
|
m = Money.from_cents('123', :gbp) # => #<Money fractional:123 currency:GBP>
|
|
@@ -474,23 +470,31 @@ Money.from_amount(2.34567).format #=> "$2.34567"
|
|
|
474
470
|
|
|
475
471
|
To round to the nearest cent (or anything more precise), you can use the `round` method. However, note that the `round` method on a `Money` object does not work the same way as a normal Ruby `Float` object. Money's `round` method accepts different arguments. The first argument to the round method is the rounding mode, while the second argument is the level of precision relative to the cent.
|
|
476
472
|
|
|
477
|
-
```
|
|
473
|
+
```ruby
|
|
478
474
|
# Float
|
|
479
475
|
2.34567.round #=> 2
|
|
480
476
|
2.34567.round(2) #=> 2.35
|
|
481
477
|
|
|
482
478
|
# Money
|
|
483
479
|
Money.default_infinite_precision = true
|
|
484
|
-
Money.from_cents(2.34567).format
|
|
480
|
+
Money.from_cents(2.34567).format #=> "$0.0234567"
|
|
485
481
|
Money.from_cents(2.34567).round.format #=> "$0.02"
|
|
486
|
-
Money.from_cents(2.34567).round(BigDecimal::
|
|
482
|
+
Money.from_cents(2.34567).round(BigDecimal::ROUND_DOWN, 2).format #=> "$0.0234"
|
|
487
483
|
```
|
|
488
484
|
|
|
489
485
|
You can set the default rounding mode by passing one of the `BigDecimal` mode enumerables like so:
|
|
486
|
+
|
|
490
487
|
```ruby
|
|
491
488
|
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
|
|
492
489
|
```
|
|
493
|
-
|
|
490
|
+
|
|
491
|
+
See [BigDecimal::ROUND_MODE](https://ruby-doc.org/3.4.1/gems/bigdecimal/BigDecimal.html#ROUND_MODE) for more information.
|
|
492
|
+
|
|
493
|
+
To round to the nearest cash value in currencies without small denominations:
|
|
494
|
+
|
|
495
|
+
```ruby
|
|
496
|
+
Money.from_cents(11_11, "CHF").to_nearest_cash_value.format # => "CHF 11.10"
|
|
497
|
+
```
|
|
494
498
|
|
|
495
499
|
## Ruby on Rails
|
|
496
500
|
|
|
@@ -506,7 +510,7 @@ In order to localize formatting you can use `I18n` gem:
|
|
|
506
510
|
Money.locale_backend = :i18n
|
|
507
511
|
```
|
|
508
512
|
|
|
509
|
-
With this enabled a thousands
|
|
513
|
+
With this enabled a thousands separator and a decimal mark will get looked up in your `I18n` translation files. In a Rails application this may look like:
|
|
510
514
|
|
|
511
515
|
```yml
|
|
512
516
|
# config/locale/en.yml
|
|
@@ -530,7 +534,7 @@ This will work seamlessly with [rails-i18n](https://github.com/svenfuchs/rails-i
|
|
|
530
534
|
|
|
531
535
|
If you wish to disable this feature and use defaults instead:
|
|
532
536
|
|
|
533
|
-
```
|
|
537
|
+
```ruby
|
|
534
538
|
Money.locale_backend = nil
|
|
535
539
|
```
|
|
536
540
|
|
|
@@ -598,7 +602,7 @@ If you don't have some locale and don't want to get a runtime error such as:
|
|
|
598
602
|
I18n::InvalidLocale: :en is not a valid locale
|
|
599
603
|
|
|
600
604
|
Set the following:
|
|
601
|
-
```
|
|
605
|
+
```ruby
|
|
602
606
|
I18n.enforce_available_locales = false
|
|
603
607
|
```
|
|
604
608
|
|
|
@@ -606,18 +610,11 @@ I18n.enforce_available_locales = false
|
|
|
606
610
|
|
|
607
611
|
Prior to v6.9.0 heuristic analysis of string input was part of this gem. Since then it was extracted in to [money-heuristics gem](https://github.com/RubyMoney/money-heuristics).
|
|
608
612
|
|
|
609
|
-
##
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
* The first is to test your application and where applicable update the
|
|
618
|
-
application to accept a `BigDecimal` return value. This is the recommended
|
|
619
|
-
path.
|
|
620
|
-
* The second is to migrate from the `#amount` and `#dollars` methods to use
|
|
621
|
-
the `#to_f` method instead. This option should only be used where `Float`
|
|
622
|
-
is the desired type and nothing else will do for your application's
|
|
623
|
-
requirements.
|
|
613
|
+
## Upgrade Guides
|
|
614
|
+
|
|
615
|
+
When upgrading between major versions, please refer to the appropriate upgrade guide:
|
|
616
|
+
|
|
617
|
+
- **[Upgrading to 7.0](UPGRADING-7.0.md)** - Guide for migrating from 6.x to 7.0
|
|
618
|
+
- **[Upgrading to 6.0](UPGRADING-6.0.md)** - Guide for upgrading to version 6.0
|
|
619
|
+
|
|
620
|
+
These guides provide detailed information about breaking changes, new features, and step-by-step migration instructions.
|