diff --git a/CHANGELOG.md b/CHANGELOG.md index 000bc8422a..519ff8fabe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Fix `Money::Allocation.generate` converting a Rational with unlimited `to_d` precision. It now uses `Money.conversion_precision` - Fix `Money::Currency.reset!` keeping the cached key set, so a currency registered before the reset was still accepted by `Money::Currency.new` and raised `NoMethodError` instead of `UnknownCurrency` - Fix `Money::Bank::VariableExchange#import_rates` parsing `:json` with `JSON.load`, which instantiates any class named by a `json_class` key; it now uses `JSON.parse` - Fix deprecation warning for eql? diff --git a/lib/money/money/allocation.rb b/lib/money/money/allocation.rb index 9ef208a7f8..4bfa14e0eb 100644 --- a/lib/money/money/allocation.rb +++ b/lib/money/money/allocation.rb @@ -79,7 +79,7 @@ def self.convert_to_big_decimal(number) if number.is_a? BigDecimal number elsif number.is_a? Rational - BigDecimal(number.to_f.to_s) + number.to_d(Money.conversion_precision) else BigDecimal(number.to_s) end diff --git a/spec/money/allocation_spec.rb b/spec/money/allocation_spec.rb index 264a27ced0..c3e6f8aaff 100644 --- a/spec/money/allocation_spec.rb +++ b/spec/money/allocation_spec.rb @@ -42,6 +42,12 @@ expect(described_class.generate(BigDecimal(5), 4, false)).to eq([1.25, 1.25, 1.25, 1.25]) end + it "converts a rational with Money.conversion_precision instead of through a float" do + expect(described_class.generate(Rational(2, 3), 1, false)).to eq([BigDecimal("0.6666666666666667")]) + expect(described_class.generate(Rational(10**400, 1), 1, false)).to eq([BigDecimal("1e400")]) + expect(described_class.generate(Rational(1, 10**400), 1, false)).to eq([BigDecimal("1e-400")]) + end + it "handles splits into repeating decimals" do amount = BigDecimal(100) parts = described_class.generate(amount, 3, false)