Browse Source

fix(ui): clamp FIRE calculator periods for already-met zero-interest goal

The zero-interest (r === 0) branch of calculatePeriodsToRetire returned
a negative number of periods when the FIRE goal was already met
(totalAmount <= P), since the already-met guard only existed on the
r !== 0 branch. This rendered a past/invalid retirement date.

Clamp the zero-interest result with Math.max(0, ...) so it can never go
negative, mirroring the existing guard's behavior on the other branch.
pull/7069/head
Vijay Sai 4 weeks ago
parent
commit
95ab429f08
  1. 1
      CHANGELOG.md
  2. 32
      libs/ui/src/lib/fire-calculator/fire-calculator.service.spec.ts
  3. 2
      libs/ui/src/lib/fire-calculator/fire-calculator.service.ts

1
CHANGELOG.md

@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fixed a negative number of periods returned by the _FIRE_ calculator when the goal is already met with no interest rate
- Fixed an issue with the localization of the country names - Fixed an issue with the localization of the country names
## 3.12.0 - 2026-06-17 ## 3.12.0 - 2026-06-17

32
libs/ui/src/lib/fire-calculator/fire-calculator.service.spec.ts

@ -32,6 +32,38 @@ describe('FireCalculatorService', () => {
expect(periodsToRetire).toBe(9); expect(periodsToRetire).toBe(9);
}); });
it('should return 0 with no interest rate when the goal is already met', async () => {
const r = 0;
const P = 2000;
const totalAmount = 1900;
const PMT = 100;
const periodsToRetire = fireCalculatorService.calculatePeriodsToRetire({
P,
r,
PMT,
totalAmount
});
expect(periodsToRetire).toBe(0);
});
it('should return the correct positive amount of periods to retire with no interest rate', async () => {
const r = 0;
const P = 1000;
const totalAmount = 5000;
const PMT = 250;
const periodsToRetire = fireCalculatorService.calculatePeriodsToRetire({
P,
r,
PMT,
totalAmount
});
expect(periodsToRetire).toBe(16);
});
it('should return the 0 when total amount is 0', async () => { it('should return the 0 when total amount is 0', async () => {
const r = 0.05; const r = 0.05;
const P = 100000; const P = 100000;

2
libs/ui/src/lib/fire-calculator/fire-calculator.service.ts

@ -52,7 +52,7 @@ export class FireCalculatorService {
}) { }) {
if (r === 0) { if (r === 0) {
// No compound interest // No compound interest
return (totalAmount - P) / PMT; return Math.max(0, (totalAmount - P) / PMT);
} else if (totalAmount <= P) { } else if (totalAmount <= P) {
return 0; return 0;
} }

Loading…
Cancel
Save