Skip to content
Open
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ tommeier
Troels Knak-Nielsen
Tsyren Ochirov
Victor Shcherbakov
wavebyrd
Wei Zhu
Yok
Yuri Sidorov
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- 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?
- Fix `Money#remainder` returning the negated divisor instead of zero when the amount is zero or divides evenly with a different sign

## 7.1.1

Expand Down
9 changes: 4 additions & 5 deletions lib/money/money/arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,10 @@ def remainder(val)
val = val.exchange_to(currency)
end

if (fractional < 0 && val < 0) || (fractional > 0 && val > 0)
modulo(val)
else
modulo(val) - (val.is_a?(Money) ? val : dup_with(fractional: val))
end
mod = modulo(val)
return mod if mod.zero? || (fractional < 0 && val < 0) || (fractional > 0 && val > 0)

mod - (val.is_a?(Money) ? val : dup_with(fractional: val))
end

# Return absolute value of self as a new Money object.
Expand Down
2 changes: 2 additions & 0 deletions spec/money/arithmetic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,8 @@
{ a: Money.new(13, :USD), b: -4, c: Money.new(1, :USD) },
{ a: Money.new(-13, :USD), b: 4, c: Money.new(-1, :USD) },
{ a: Money.new(-13, :USD), b: -4, c: Money.new(-1, :USD) },
{ a: Money.new(-12, :USD), b: 4, c: Money.new(0, :USD) },
{ a: Money.new(0, :USD), b: 4, c: Money.new(0, :USD) },
]
ts.each do |t|
expect(t[:a].remainder(t[:b])).to eq t[:c]
Expand Down
Loading