money 6.19.0 → 7.0.1
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 +101 -47
- data/LICENSE +1 -1
- data/README.md +57 -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 +49 -33
- data/lib/money/money/constructors.rb +2 -0
- data/lib/money/money/formatter.rb +14 -63
- 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 +12 -5
- metadata +43 -24
- 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: b617ca145c64fc7269e5bdc8f3bb64d8c7e18fe081ac568c1b9f897f89e1fee1
|
|
4
|
+
data.tar.gz: 5d5172afa73dfbb249b14e8540de83e469633b50c23f664108b2159d8dc7ec09
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: af420e10767bd715a6126b658bc9f934a0087e4535a2114fbc342b40b7ad9a85d9ebe56d35e00756034f42ed76172fa999134e5aa29132e2612845073ac60d28
|
|
7
|
+
data.tar.gz: 2f8360e47d12a96e58ec590e1b53fbb5b2f3ba870d362151b3a31e17474477aebcc5c9e97c3b5b66c24c4613e00a06a14cb65088a00a03a7e44296c1e35b2545
|
data/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,65 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
-
##
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 7.0.1
|
|
6
|
+
|
|
7
|
+
- Re-publishing gem to fix file permissions https://github.com/RubyMoney/money/issues/1170
|
|
8
|
+
|
|
9
|
+
## 7.0.0
|
|
10
|
+
|
|
11
|
+
- **Breaking change**: Require Ruby >= 3.1 and i18n ~> 1.9
|
|
12
|
+
- **Breaking change**: Remove deprecated formatting rules:
|
|
13
|
+
- `:html`
|
|
14
|
+
- `:html_wrap_symbol`
|
|
15
|
+
- `:symbol_position`
|
|
16
|
+
- `:symbol_before_without_space`
|
|
17
|
+
- `:symbol_after_without_space`
|
|
18
|
+
- **Breaking change**: Remove deprecated methods:
|
|
19
|
+
- `Money.infinite_precision`.
|
|
20
|
+
- `Money.infinite_precision=`.
|
|
21
|
+
- `Money#currency_as_string`.
|
|
22
|
+
- `Money#currency_as_string=`.
|
|
23
|
+
- `Money.use_i18n` and `Money.use_i18n=` (use `Money.locale_backend = :i18n` instead).
|
|
24
|
+
- **Breaking change**: Remove legacy locale backend. The default locale backend is now `:currency` which uses currency definitions for formatting.
|
|
25
|
+
- **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.
|
|
26
|
+
- **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.
|
|
27
|
+
- **Potential breaking change**: Fix RSD (Serbian Dinar) formatting to be like `12.345,42 RSD`
|
|
28
|
+
- **Potential breaking change**: Fix USDC decimals places from 2 to 6
|
|
29
|
+
- **Potential breaking change**: Fix MGA (Malagasy Ariary) to be a zero-decimal currency (changing subunit_to_unit from 5 to 1)
|
|
30
|
+
- **Potential breaking change**: Remove special handling for Japanese language only
|
|
31
|
+
- **Potential breaking change**: Adjust formatting rules to use i18n translations for `:format`
|
|
32
|
+
- **Potential breaking change**: Not allow divide by zero
|
|
33
|
+
- Updated Armenian Dram sign and HTML entity
|
|
34
|
+
- Updated the Turkmen Manat symbol and HTML entity and added disambiguation symbol for TMM
|
|
35
|
+
- Expose Money::VERSION
|
|
36
|
+
- Fix typo in ILS currency
|
|
37
|
+
- Add Zimbabwe Gold (ZWG) currency
|
|
38
|
+
- Update thousands_separator for CHF
|
|
39
|
+
- Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG)
|
|
40
|
+
- Add `Money.strict_eql_compare = true` so that comparing zero amounts with different currencies using `Money#eql?` returns `false`
|
|
41
|
+
```rb
|
|
42
|
+
Money.new(0, "USD").eql?(Money.new(0, "EUR")) #=> true
|
|
43
|
+
#> [DEPRECATION] Comparing 0 USD with 0 EUR using `#eql?` will return false…
|
|
44
|
+
|
|
45
|
+
Money.strict_eql_compare = true
|
|
46
|
+
Money.new(0, "USD").eql?(Money.new(0, "EUR")) #=> false
|
|
47
|
+
```
|
|
48
|
+
- Add `Money#to_nearest_cash_value` to return a rounded Money instance to the smallest denomination
|
|
49
|
+
- Deprecate `Money#round_to_nearest_cash_value` in favor of calling `to_nearest_cash_value.fractional`
|
|
50
|
+
- Deprecate `Money#dollars` in favor of `Money#amount`.
|
|
51
|
+
- Deprecate `Money.from_dollars` in favor of `Money.from_amount`.
|
|
52
|
+
- Add `Money::Currency#cents_based?` to check if currency is cents-based
|
|
53
|
+
- Add ability to nest `Money.with_rounding_mode` blocks
|
|
54
|
+
- Allow `nil` to be used as a default_currency
|
|
55
|
+
- Improve performance of `find_by_iso_numeric` method
|
|
56
|
+
- Add ability to nest `Money.with_bank` blocks
|
|
57
|
+
- 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
58
|
|
|
7
59
|
## 6.19.0
|
|
8
60
|
|
|
9
61
|
- Change Peruvian Sol (PEN) decimal mark and thousands separator.
|
|
62
|
+
- Fix deprecation warning for BigDecimal being moved out from stdlib.
|
|
10
63
|
- Improves Precision and Simplifies Allocation Logic
|
|
11
64
|
- explicit declaration of i18n locales
|
|
12
65
|
- Add space to CHF format
|
|
@@ -15,6 +68,7 @@
|
|
|
15
68
|
## 6.18.0
|
|
16
69
|
|
|
17
70
|
- Add second dobra (STN) from São Tomé and Príncipe
|
|
71
|
+
- Add Sierra Leonean (new) leone (SLE) from Sierra Leone
|
|
18
72
|
- Correct South African Rand (ZAR) to use comma decimal mark, and space thousands separator
|
|
19
73
|
- Use euro symbol as html_entity for euro currency
|
|
20
74
|
- Update Georgian Lari symbol
|
|
@@ -211,7 +265,7 @@
|
|
|
211
265
|
return `nil` as `Comparable#==` will not rescue exceptions in the next release.
|
|
212
266
|
- Fix `Currency` specs for `#exponent` and `#decimal_places` not making assertions.
|
|
213
267
|
- Fix a couple of Ruby warnings found in specs.
|
|
214
|
-
- Fix `Money#-`,`Money#+`
|
|
268
|
+
- Fix `Money#-`,`Money#+` arithmetic for Ruby 2.3+ : check for zero value without using eql? with a Fixnum. (#577)
|
|
215
269
|
- Use `Money#decimal_mark` when formatting with `rounded_infinite_precision` rule
|
|
216
270
|
set to `true`.
|
|
217
271
|
- Replaced meta-defined `thousands_separator` and `decimal_mark` methods with regular methods. (#579)
|
|
@@ -258,7 +312,7 @@
|
|
|
258
312
|
- Fix formatting of NGN - show symbol before amount
|
|
259
313
|
- Switch default and alternate symbols for RUB currency
|
|
260
314
|
- Fix symbol for TRY currency
|
|
261
|
-
- Add `Money.default_formatting_rules` hash, meant to define default rules for
|
|
315
|
+
- 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
316
|
- Add support for the new official symbol for Russian Ruble (RUB) — «₽»
|
|
263
317
|
|
|
264
318
|
## 6.2.1
|
|
@@ -427,7 +481,7 @@ Features
|
|
|
427
481
|
|
|
428
482
|
Bugfixes
|
|
429
483
|
--------
|
|
430
|
-
- EEK currency is no longer used, kept for BC ([#issue/110](
|
|
484
|
+
- EEK currency is no longer used, kept for BC ([#issue/110](https://github.com/RubyMoney/money/issues/110))
|
|
431
485
|
- Lithuanian Litas symbol position fixed (laurynas)
|
|
432
486
|
- Fixed README typos (phlipper)
|
|
433
487
|
- Fixed README typos (pwim)
|
|
@@ -553,11 +607,11 @@ Bugfixes
|
|
|
553
607
|
--------
|
|
554
608
|
- Updated Money#to_s to respect :separator and :subunit_to_unit.
|
|
555
609
|
- Fixed Money#format for :subunit_to_unit != 100.
|
|
556
|
-
([#issue/37](
|
|
610
|
+
([#issue/37](https://github.com/RubyMoney/money/issues/37))
|
|
557
611
|
- Fixed String#to_money for :subunit_to_unit != 100.
|
|
558
|
-
([#issue/36](
|
|
612
|
+
([#issue/36](https://github.com/RubyMoney/money/issues/36))
|
|
559
613
|
- Removed duplicate currencies.
|
|
560
|
-
([#issue/38](
|
|
614
|
+
([#issue/38](https://github.com/RubyMoney/money/issues/38))
|
|
561
615
|
- Fixed issue related to JRuby returning 2 for Math.log10(1000).floor instead
|
|
562
616
|
of correctly returning 3.
|
|
563
617
|
|
|
@@ -568,22 +622,22 @@ Features
|
|
|
568
622
|
--------
|
|
569
623
|
- Added support for creating objects with the main monetary unit instead of
|
|
570
624
|
cents.
|
|
571
|
-
([#issue/25](
|
|
625
|
+
([#issue/25](https://github.com/RubyMoney/money/issues/25))
|
|
572
626
|
- Deprecated `Money#format` with separate params instead of Hash. Deprecation
|
|
573
627
|
target set to Money 3.5.0.
|
|
574
|
-
([#issue/31](
|
|
628
|
+
([#issue/31](https://github.com/RubyMoney/money/issues/31))
|
|
575
629
|
- Deprecated `Money#new(0, currency: "EUR")` in favor of
|
|
576
630
|
`Money#new(0, "EUR")`. Deprecation target set to Money 3.5.0.
|
|
577
|
-
([#issue/31](
|
|
631
|
+
([#issue/31](https://github.com/RubyMoney/money/issues/31))
|
|
578
632
|
- Throw ArgumentError when trying to multiply two Money objects together.
|
|
579
|
-
([#issue/29](
|
|
633
|
+
([#issue/29](https://github.com/RubyMoney/money/issues/29))
|
|
580
634
|
- Update Money#parse to use :subunit_to_unit
|
|
581
|
-
([#issue/30](
|
|
635
|
+
([#issue/30](https://github.com/RubyMoney/money/issues/30))
|
|
582
636
|
|
|
583
637
|
Bugfixes
|
|
584
638
|
--------
|
|
585
639
|
- Downgraded required_rubygems_version to >= 1.3.6.
|
|
586
|
-
([#issue/26](
|
|
640
|
+
([#issue/26](https://github.com/RubyMoney/money/issues/26))
|
|
587
641
|
- Use BigDecimal when floating point calculations are needed.
|
|
588
642
|
- Ruby 1.9.2 compatibility enhancements.
|
|
589
643
|
|
|
@@ -593,32 +647,32 @@ Money 3.1.0
|
|
|
593
647
|
Features
|
|
594
648
|
--------
|
|
595
649
|
- Implemented `Money::Bank::Base`.
|
|
596
|
-
([#issue/14](
|
|
650
|
+
([#issue/14](https://github.com/RubyMoney/money/issues/14))
|
|
597
651
|
- Added `Money::Bank::Base#exchange_with`.
|
|
598
652
|
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
|
599
653
|
3.2.0.
|
|
600
|
-
-
|
|
654
|
+
- Implemented `Money::Bank::VariableExchange`
|
|
601
655
|
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
|
602
656
|
3.2.0.
|
|
603
657
|
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
|
604
658
|
Deprecation target set to Money 3.2.0.
|
|
605
|
-
([#issue/16](
|
|
659
|
+
([#issue/16](https://github.com/RubyMoney/money/issues/16))
|
|
606
660
|
- Implemented `#has` for `Money` and `Money::Currency`.
|
|
607
661
|
- Refactored test suite to conform to RSpec conventions.
|
|
608
|
-
- Moved project from [FooBarWidget](
|
|
609
|
-
[RubyMoney](
|
|
662
|
+
- Moved project from [FooBarWidget](https://github.com/FooBarWidget) to
|
|
663
|
+
[RubyMoney](https://github.com/RubyMoney)
|
|
610
664
|
- Added Simone Carletti to list of authors.
|
|
611
665
|
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
|
612
666
|
`Money::Bank::Base`.
|
|
613
|
-
([#issue/18](
|
|
667
|
+
([#issue/18](https://github.com/RubyMoney/money/issues/18))
|
|
614
668
|
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
|
615
|
-
([#issue/19](
|
|
616
|
-
- Added [google_currency](
|
|
669
|
+
([#issue/19](https://github.com/RubyMoney/money/issues/19))
|
|
670
|
+
- Added [google_currency](https://github.com/RubyMoney/google_currency) to list
|
|
617
671
|
of Currency Exchange Implementations.
|
|
618
672
|
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
|
619
|
-
([#issue/21](
|
|
673
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
620
674
|
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
|
621
|
-
([#issue/21](
|
|
675
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
622
676
|
- Removed dependency on Jeweler.
|
|
623
677
|
- Replaced usage of hanna with yardoc.
|
|
624
678
|
- Rewrote/reformatted all documentation.
|
|
@@ -626,30 +680,30 @@ Features
|
|
|
626
680
|
Bugfixes
|
|
627
681
|
--------
|
|
628
682
|
- Fixed incorrect URLs in documentation.
|
|
629
|
-
([#issue/17](
|
|
683
|
+
([#issue/17](https://github.com/RubyMoney/money/issues/17))
|
|
630
684
|
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
|
631
|
-
([#issue/20](
|
|
685
|
+
([#issue/20](https://github.com/RubyMoney/money/issues/20))
|
|
632
686
|
- Updated Ghanaian Cedi to use correct ISO Code, GHS.
|
|
633
|
-
([#issue/22](
|
|
687
|
+
([#issue/22](https://github.com/RubyMoney/money/issues/22))
|
|
634
688
|
- Make `default` rake task call `spec`.
|
|
635
|
-
([#issue/23](
|
|
689
|
+
([#issue/23](https://github.com/RubyMoney/money/issues/23))
|
|
636
690
|
|
|
637
691
|
Money 3.1.0.pre3
|
|
638
692
|
================
|
|
639
693
|
|
|
640
694
|
Features
|
|
641
695
|
--------
|
|
642
|
-
- Added [google_currency](
|
|
696
|
+
- Added [google_currency](https://github.com/RubyMoney/google_currency) to list
|
|
643
697
|
of Currency Exchange Implementations.
|
|
644
698
|
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
|
645
|
-
([#issue/21](
|
|
699
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
646
700
|
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
|
647
|
-
([#issue/21](
|
|
701
|
+
([#issue/21](https://github.com/RubyMoney/money/issues/21))
|
|
648
702
|
|
|
649
703
|
Bugfixes
|
|
650
704
|
--------
|
|
651
705
|
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
|
652
|
-
([#issue/20](
|
|
706
|
+
([#issue/20](https://github.com/RubyMoney/money/issues/20))
|
|
653
707
|
|
|
654
708
|
Money 3.1.0.pre2
|
|
655
709
|
================
|
|
@@ -658,14 +712,14 @@ Features
|
|
|
658
712
|
--------
|
|
659
713
|
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
|
660
714
|
`Money::Bank::Base`.
|
|
661
|
-
([#issue/18](
|
|
715
|
+
([#issue/18](https://github.com/RubyMoney/money/issues/18))
|
|
662
716
|
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
|
663
|
-
([#issue/19](
|
|
717
|
+
([#issue/19](https://github.com/RubyMoney/money/issues/19))
|
|
664
718
|
|
|
665
719
|
Bugfixes
|
|
666
720
|
--------
|
|
667
721
|
- Fixed incorrect URLs in documentation.
|
|
668
|
-
([#issue/17](
|
|
722
|
+
([#issue/17](https://github.com/RubyMoney/money/issues/17))
|
|
669
723
|
|
|
670
724
|
Money 3.1.0.pre1
|
|
671
725
|
================
|
|
@@ -673,26 +727,26 @@ Money 3.1.0.pre1
|
|
|
673
727
|
Features
|
|
674
728
|
--------
|
|
675
729
|
- Implemented `Money::Bank::Base`.
|
|
676
|
-
([#issue/14](
|
|
730
|
+
([#issue/14](https://github.com/RubyMoney/money/issues/14))
|
|
677
731
|
- Added `Money::Bank::Base#exchange_with`.
|
|
678
732
|
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
|
679
733
|
3.2.0.
|
|
680
|
-
-
|
|
734
|
+
- Implemented `Money::Bank::VariableExchange`
|
|
681
735
|
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
|
682
736
|
3.2.0.
|
|
683
737
|
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
|
684
738
|
Deprecation target set to Money 3.2.0.
|
|
685
|
-
([#issue/16](
|
|
739
|
+
([#issue/16](https://github.com/RubyMoney/money/issues/16))
|
|
686
740
|
- Implemented `#has` for `Money` and `Money::Currency`.
|
|
687
741
|
- Refactored test suite to conform to RSpec conventions.
|
|
688
|
-
- Moved project from [FooBarWidget](
|
|
689
|
-
[RubyMoney](
|
|
742
|
+
- Moved project from [FooBarWidget](https://github.com/FooBarWidget) to
|
|
743
|
+
[RubyMoney](https://github.com/RubyMoney)
|
|
690
744
|
- Added Simone Carletti to list of authors.
|
|
691
745
|
|
|
692
746
|
Bugfixes
|
|
693
747
|
--------
|
|
694
748
|
- Fixed rounding error in `Numeric#to_money`.
|
|
695
|
-
([#issue/15](
|
|
749
|
+
([#issue/15](https://github.com/RubyMoney/money/issues/15))
|
|
696
750
|
|
|
697
751
|
Money 3.0.5
|
|
698
752
|
===========
|
|
@@ -703,11 +757,11 @@ Features
|
|
|
703
757
|
- Added ability to pass a block to `VariableExchangeBank#new` or `#exchange`,
|
|
704
758
|
specifying a custom truncation method
|
|
705
759
|
- Added optional `currency` argument to` Numeric#to_money`.
|
|
706
|
-
([#issue/11](
|
|
760
|
+
([#issue/11](https://github.com/RubyMoney/money/issues/11))
|
|
707
761
|
- Added optional `currency` argument to `String#to_money`.
|
|
708
|
-
([#issue/11](
|
|
762
|
+
([#issue/11](https://github.com/RubyMoney/money/issues/11))
|
|
709
763
|
- Use '¤' as the default currency symbol.
|
|
710
|
-
([#issue/10](
|
|
764
|
+
([#issue/10](https://github.com/RubyMoney/money/issues/10))
|
|
711
765
|
|
|
712
766
|
Bugfixes
|
|
713
767
|
--------
|
|
@@ -716,7 +770,7 @@ Bugfixes
|
|
|
716
770
|
- Fixed issue when exchanging currencies with different `:subunit_to_unit`
|
|
717
771
|
values.
|
|
718
772
|
- `Numeric#to_money` now respects `:subunit_to_unit`.
|
|
719
|
-
([#issue/12](
|
|
773
|
+
([#issue/12](https://github.com/RubyMoney/money/issues/12))
|
|
720
774
|
|
|
721
775
|
Money 3.0.4
|
|
722
776
|
===========
|
|
@@ -763,7 +817,7 @@ Features
|
|
|
763
817
|
--------
|
|
764
818
|
- Version Bump due to compatibility changes with ActiveRecord. See
|
|
765
819
|
conversation
|
|
766
|
-
[here](
|
|
820
|
+
[here](https://github.com/RubyMoney/money/issues/4#issuecomment-224880)
|
|
767
821
|
for more information.
|
|
768
822
|
|
|
769
823
|
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
|
-
|
|
8
|
+
⚠️ 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,15 +444,21 @@ 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>
|
|
457
453
|
m.format(symbol: m.currency.to_s + ' ') # => "GBP 1.23"
|
|
458
454
|
```
|
|
459
455
|
|
|
456
|
+
If you would like to customize currency symbols to avoid ambiguity between currencies, you can:
|
|
457
|
+
|
|
458
|
+
```ruby
|
|
459
|
+
Money::Currency.table[:hkd][:symbol] = 'HK$'
|
|
460
|
+
```
|
|
461
|
+
|
|
460
462
|
## Rounding
|
|
461
463
|
|
|
462
464
|
By default, `Money` objects are rounded to the nearest cent and the additional precision is not preserved:
|
|
@@ -474,23 +476,31 @@ Money.from_amount(2.34567).format #=> "$2.34567"
|
|
|
474
476
|
|
|
475
477
|
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
478
|
|
|
477
|
-
```
|
|
479
|
+
```ruby
|
|
478
480
|
# Float
|
|
479
481
|
2.34567.round #=> 2
|
|
480
482
|
2.34567.round(2) #=> 2.35
|
|
481
483
|
|
|
482
484
|
# Money
|
|
483
485
|
Money.default_infinite_precision = true
|
|
484
|
-
Money.from_cents(2.34567).format
|
|
486
|
+
Money.from_cents(2.34567).format #=> "$0.0234567"
|
|
485
487
|
Money.from_cents(2.34567).round.format #=> "$0.02"
|
|
486
|
-
Money.from_cents(2.34567).round(BigDecimal::
|
|
488
|
+
Money.from_cents(2.34567).round(BigDecimal::ROUND_DOWN, 2).format #=> "$0.0234"
|
|
487
489
|
```
|
|
488
490
|
|
|
489
491
|
You can set the default rounding mode by passing one of the `BigDecimal` mode enumerables like so:
|
|
492
|
+
|
|
490
493
|
```ruby
|
|
491
494
|
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
|
|
492
495
|
```
|
|
493
|
-
|
|
496
|
+
|
|
497
|
+
See [BigDecimal::ROUND_MODE](https://ruby-doc.org/3.4.1/gems/bigdecimal/BigDecimal.html#ROUND_MODE) for more information.
|
|
498
|
+
|
|
499
|
+
To round to the nearest cash value in currencies without small denominations:
|
|
500
|
+
|
|
501
|
+
```ruby
|
|
502
|
+
Money.from_cents(11_11, "CHF").to_nearest_cash_value.format # => "CHF 11.10"
|
|
503
|
+
```
|
|
494
504
|
|
|
495
505
|
## Ruby on Rails
|
|
496
506
|
|
|
@@ -506,7 +516,7 @@ In order to localize formatting you can use `I18n` gem:
|
|
|
506
516
|
Money.locale_backend = :i18n
|
|
507
517
|
```
|
|
508
518
|
|
|
509
|
-
With this enabled a thousands
|
|
519
|
+
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
520
|
|
|
511
521
|
```yml
|
|
512
522
|
# config/locale/en.yml
|
|
@@ -530,7 +540,7 @@ This will work seamlessly with [rails-i18n](https://github.com/svenfuchs/rails-i
|
|
|
530
540
|
|
|
531
541
|
If you wish to disable this feature and use defaults instead:
|
|
532
542
|
|
|
533
|
-
```
|
|
543
|
+
```ruby
|
|
534
544
|
Money.locale_backend = nil
|
|
535
545
|
```
|
|
536
546
|
|
|
@@ -598,7 +608,7 @@ If you don't have some locale and don't want to get a runtime error such as:
|
|
|
598
608
|
I18n::InvalidLocale: :en is not a valid locale
|
|
599
609
|
|
|
600
610
|
Set the following:
|
|
601
|
-
```
|
|
611
|
+
```ruby
|
|
602
612
|
I18n.enforce_available_locales = false
|
|
603
613
|
```
|
|
604
614
|
|
|
@@ -606,18 +616,11 @@ I18n.enforce_available_locales = false
|
|
|
606
616
|
|
|
607
617
|
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
618
|
|
|
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.
|
|
619
|
+
## Upgrade Guides
|
|
620
|
+
|
|
621
|
+
When upgrading between major versions, please refer to the appropriate upgrade guide:
|
|
622
|
+
|
|
623
|
+
- [Upgrading to 7.0](https://github.com/RubyMoney/money/blob/main/UPGRADING-7.0.md) - Guide for migrating from 6.x to 7.0
|
|
624
|
+
- [Upgrading to 6.0](https://github.com/RubyMoney/money/blob/main/UPGRADING-6.0.md) - Guide for upgrading to version 6.0
|
|
625
|
+
|
|
626
|
+
These guides provide detailed information about breaking changes, new features, and step-by-step migration instructions.
|