Skip to content

Commit

Permalink
Merge pull request #688 from bertiecroll/fix-money-only-cents-for-neg…
Browse files Browse the repository at this point in the history
…ative-money

Fix money_only_cents for negative money
  • Loading branch information
semmons99 authored Mar 13, 2024
2 parents 0815c28 + 1868b87 commit 72c3773
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Allow monetizing methods with kwargs
- Fix money_only_cents for negative money

## 1.15.0

Expand Down
2 changes: 1 addition & 1 deletion lib/money-rails/helpers/action_view_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def money_only_cents(value)

value = value.to_money

format "%0#{value.currency.exponent}d", (value % value.currency.subunit_to_unit).cents
format "%0#{value.currency.exponent}d", (value.abs % value.currency.subunit_to_unit).cents
end
end
end
5 changes: 5 additions & 0 deletions spec/helpers/action_view_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
let(:monetizable_object){ false }
it { is_expected.to eq "00" }
end

context 'with a negative monetizable object' do
let(:monetizable_object){ Money.new(-1_25) }
it { is_expected.to eq "25" }
end
end

context 'respects MoneyRails::Configuration settings' do
Expand Down

0 comments on commit 72c3773

Please sign in to comment.