Skip to content

Commit 72c3773

Browse files
authored
Merge pull request #688 from bertiecroll/fix-money-only-cents-for-negative-money
Fix money_only_cents for negative money
2 parents 0815c28 + 1868b87 commit 72c3773

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Allow monetizing methods with kwargs
6+
- Fix money_only_cents for negative money
67

78
## 1.15.0
89

lib/money-rails/helpers/action_view_extension.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def money_only_cents(value)
5353

5454
value = value.to_money
5555

56-
format "%0#{value.currency.exponent}d", (value % value.currency.subunit_to_unit).cents
56+
format "%0#{value.currency.exponent}d", (value.abs % value.currency.subunit_to_unit).cents
5757
end
5858
end
5959
end

spec/helpers/action_view_extension_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@
105105
let(:monetizable_object){ false }
106106
it { is_expected.to eq "00" }
107107
end
108+
109+
context 'with a negative monetizable object' do
110+
let(:monetizable_object){ Money.new(-1_25) }
111+
it { is_expected.to eq "25" }
112+
end
108113
end
109114

110115
context 'respects MoneyRails::Configuration settings' do

0 commit comments

Comments
 (0)