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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
2 changes: 1 addition & 1 deletion lib/money/money/allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/money/allocation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading