Skip to content

Narrow RoundingNecessaryException for Money::of() with int amount#14

Merged
simPod merged 1 commit intomainfrom
narrow-zero-amount-rounding
Mar 13, 2026
Merged

Narrow RoundingNecessaryException for Money::of() with int amount#14
simPod merged 1 commit intomainfrom
narrow-zero-amount-rounding

Conversation

@simPod
Copy link
Owner

@simPod simPod commented Mar 13, 2026

Summary

  • Extends the zero-amount narrowing from Narrow RoundingNecessaryException for Money::of() with zero amount #13 to cover all integer amounts when the context doesn't involve step division
  • When Money::of() is called with an int amount, scaling to higher precision just adds trailing zeros — no rounding needed
  • Safe contexts: no context arg (default DefaultContext), new DefaultContext(), new AutoContext(), new CustomContext($scale), new CustomContext($scale, 1)
  • CustomContext with step > 1 is still treated as potentially throwing (division by step can require rounding)

Fixes e.g. Money::of(500, 'USD', new CustomContext(4, 1)) being flagged as throwing RoundingNecessaryException.

Test plan

  • Money::of(100, 'USD') — int + default context = no throw
  • Money::of(100, $currency) — int + Currency instance = no throw
  • Money::of(0, 'EUR', new CustomContext(4, 1)) — zero = no throw
  • Money::of(0, $currency) — zero + safe currency = no throw
  • Money::of(500, 'EUR', new CustomContext(4, 1)) — int + step 1 = no throw
  • Money::of(500, 'EUR', new CustomContext(4)) — int + default step = no throw
  • Money::of(500, 'EUR', new CustomContext(2, 5)) — int + step > 1 = may throw
  • Money::of($string, 'EUR', new CustomContext(4, 1)) — string amount = may throw
  • All existing tests pass

@simPod simPod force-pushed the narrow-zero-amount-rounding branch from b72549c to e58413e Compare March 13, 2026 15:18
When the amount is int and the context only scales (no step division),
Money::of() cannot throw RoundingNecessaryException because scaling
an integer to higher precision just adds trailing zeros.
@simPod simPod force-pushed the narrow-zero-amount-rounding branch from e58413e to 78a4c99 Compare March 13, 2026 15:20
@simPod simPod merged commit 8239e26 into main Mar 13, 2026
8 checks passed
@simPod simPod deleted the narrow-zero-amount-rounding branch March 13, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant