Skip to content

Commit 377b0e8

Browse files
authored
Merge pull request #1144 from RubyMoney/jl-remove-rounding-mode
remove deprecations related with rounding mode
2 parents a2c4bae + dc3f52b commit 377b0e8

File tree

5 files changed

+14
-68
lines changed

5 files changed

+14
-68
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- `Money.infinite_precision`
88
- `Money.infinite_precision=`
99
- **Breaking change**: Default currency is now `nil` instead of `USD`. If you want to keep the previous behavior, set `Money.default_currency = Money::Currency.new("USD")` in your initializer. Initializing a Money object will raise a `Currency::NoCurrency` if no currency is set.
10+
- **Breaking change**: The default rounding mode has changed from `BigDecimal::ROUND_HALF_EVEN` to `BigDecimal::ROUND_HALF_UP`. Set it explicitly using `Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN` to keep the previous behavior.
1011
- **Potential breaking change**: Fix RSD (Serbian Dinar) formatting to be like `12.345,42 RSD`
1112
- **Potential breaking change**: Fix USDC decimals places from 2 to 6
1213
- **Potential breaking change**: Fix MGA (Malagasy Ariary) to be a zero-decimal currency (changing subunit_to_unit from 5 to 1)

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -478,16 +478,16 @@ To round to the nearest cent (or anything more precise), you can use the `round`
478478
479479
# Money
480480
Money.default_infinite_precision = true
481-
Money.from_cents(2.34567).format #=> "$0.0234567"
481+
Money.from_cents(2.34567).format #=> "$0.0234567"
482482
Money.from_cents(2.34567).round.format #=> "$0.02"
483-
Money.from_cents(2.34567).round(BigDecimal::ROUND_HALF_UP, 2).format #=> "$0.0235"
483+
Money.from_cents(2.34567).round(BigDecimal::ROUND_DOWN, 2).format #=> "$0.0234"
484484
```
485485

486486
You can set the default rounding mode by passing one of the `BigDecimal` mode enumerables like so:
487487
```ruby
488488
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
489489
```
490-
See [BigDecimal::ROUND_MODE](https://ruby-doc.org/stdlib-2.5.1/libdoc/bigdecimal/rdoc/BigDecimal.html#ROUND_MODE) for more information
490+
See [BigDecimal::ROUND_MODE](https://ruby-doc.org/3.4.1/gems/bigdecimal/BigDecimal.html#ROUND_MODE) for more information
491491

492492
## Ruby on Rails
493493

lib/money/money.rb

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ def self.locale_backend=(value)
168168

169169
# @attr_writer rounding_mode Use this to specify the rounding mode
170170
def self.rounding_mode=(new_rounding_mode)
171-
@using_deprecated_default_rounding_mode = false
172171
@rounding_mode = new_rounding_mode
173172
end
174173

@@ -195,9 +194,8 @@ def self.setup_defaults
195194
# Default to not using infinite precision cents
196195
self.default_infinite_precision = false
197196

198-
# Default to bankers rounding
199-
self.rounding_mode = BigDecimal::ROUND_HALF_EVEN
200-
@using_deprecated_default_rounding_mode = true
197+
# Default rounding mode toward the nearest neighbor; if the neighbors are equidistant, round away from zero
198+
self.rounding_mode = BigDecimal::ROUND_HALF_UP
201199

202200
# Default the conversion of Rationals precision to 16
203201
self.conversion_precision = 16
@@ -211,23 +209,10 @@ def self.inherited(base)
211209

212210
# Use this to return the rounding mode.
213211
#
214-
# @param [BigDecimal::ROUND_MODE] mode
215-
#
216212
# @return [BigDecimal::ROUND_MODE] rounding mode
217-
def self.rounding_mode(mode = nil)
218-
if mode
219-
warn "[DEPRECATION] calling `rounding_mode` with a block is deprecated. Please use `.with_rounding_mode` instead."
220-
return with_rounding_mode(mode) { yield }
221-
end
222-
213+
def self.rounding_mode
223214
return Thread.current[:money_rounding_mode] if Thread.current[:money_rounding_mode]
224215

225-
if @using_deprecated_default_rounding_mode
226-
warn '[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in the ' \
227-
'next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.'
228-
@using_deprecated_default_rounding_mode = false
229-
end
230-
231216
@rounding_mode
232217
end
233218

@@ -241,7 +226,7 @@ def self.rounding_mode(mode = nil)
241226
# @return [Object] block results
242227
#
243228
# @example
244-
# fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_UP) do
229+
# fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_DOWN) do
245230
# Money.new(1200) * BigDecimal('0.029')
246231
# end
247232
def self.with_rounding_mode(mode)

sig/lib/money/money.rbs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,8 @@ class Money
215215

216216
# Use this to return the rounding mode.
217217
#
218-
# @param [BigDecimal::ROUND_MODE] mode
219-
#
220218
# @return [BigDecimal::ROUND_MODE] rounding mode
221-
def self.rounding_mode: (?untyped? mode) { () -> untyped } -> untyped
219+
def self.rounding_mode: () -> untyped
222220

223221
# Temporarily changes the rounding mode in a given block.
224222
#
@@ -230,7 +228,7 @@ class Money
230228
# @return [Object] block results
231229
#
232230
# @example
233-
# fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_UP) do
231+
# fee = Money.with_rounding_mode(BigDecimal::ROUND_HALF_DOWN) do
234232
# Money.new(1200) * BigDecimal('0.029')
235233
# end
236234
def self.with_rounding_mode: (untyped mode) { () -> untyped } -> untyped

spec/money_spec.rb

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -229,23 +229,6 @@
229229
end
230230

231231
describe '.with_rounding_mode' do
232-
it 'sets the .rounding_mode method deprecated' do
233-
allow(Money).to receive(:warn)
234-
allow(Money).to receive(:with_rounding_mode).and_call_original
235-
236-
rounding_block = lambda do
237-
Money.from_amount(1.999).to_d
238-
end
239-
240-
expect(Money.from_amount(1.999).to_d).to eq 2
241-
expect(Money.rounding_mode(BigDecimal::ROUND_DOWN, &rounding_block)).to eq 1.99
242-
expect(Money)
243-
.to have_received(:warn)
244-
.with('[DEPRECATION] calling `rounding_mode` with a block is deprecated. ' \
245-
'Please use `.with_rounding_mode` instead.')
246-
expect(Money).to have_received(:with_rounding_mode).with(BigDecimal::ROUND_DOWN, &rounding_block)
247-
end
248-
249232
it 'rounds using with_rounding_mode' do
250233
expect(Money.from_amount(1.999).to_d).to eq 2
251234
expect(Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do
@@ -270,7 +253,7 @@
270253

271254
expect(
272255
Money.rounding_mode
273-
).to eq(BigDecimal::ROUND_HALF_EVEN), 'Original mode should be restored after outer block'
256+
).to eq(BigDecimal::ROUND_HALF_UP), 'Original mode should be restored after outer block'
274257
expect(Money.from_amount(2.137).to_d).to eq 2.14
275258
end
276259

@@ -323,7 +306,7 @@
323306
expect(result[:result]).to eq(expected_up)
324307
end
325308

326-
expect(Money.rounding_mode).to eq(BigDecimal::ROUND_HALF_EVEN)
309+
expect(Money.rounding_mode).to eq(BigDecimal::ROUND_HALF_UP)
327310
end
328311
end
329312

@@ -417,7 +400,7 @@ def expectation.fractional
417400
Money.new(1.1).fractional
418401
end).to eq 2
419402

420-
expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_EVEN
403+
expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_UP
421404
end
422405

423406
it "works for multiplication within a block" do
@@ -429,7 +412,7 @@ def expectation.fractional
429412
expect((Money.new(1_00) * "0.011".to_d).fractional).to eq 2
430413
end
431414

432-
expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_EVEN
415+
expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_UP
433416
end
434417
end
435418
end
@@ -1032,27 +1015,6 @@ def m.amount
10321015
end
10331016
end
10341017

1035-
describe ".rounding_mode" do
1036-
before { Money.setup_defaults }
1037-
after { Money.setup_defaults }
1038-
1039-
it 'warns about changing default rounding_mode value' do
1040-
expect(Money)
1041-
.to receive(:warn)
1042-
.with('[WARNING] The default rounding mode will change from `ROUND_HALF_EVEN` to `ROUND_HALF_UP` in ' \
1043-
'the next major release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.')
1044-
1045-
Money.rounding_mode
1046-
end
1047-
1048-
it 'does not warn if the default rounding_mode has been changed' do
1049-
Money.rounding_mode = BigDecimal::ROUND_HALF_UP
1050-
1051-
expect(Money).not_to receive(:warn)
1052-
Money.rounding_mode
1053-
end
1054-
end
1055-
10561018
describe '.default_bank' do
10571019
after { Money.setup_defaults }
10581020

0 commit comments

Comments
 (0)