diff --git a/CHANGELOG.md b/CHANGELOG.md index f964c9cae..170284f81 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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 ## 3.12.0 - 2026-06-17 diff --git a/libs/ui/src/lib/fire-calculator/fire-calculator.service.spec.ts b/libs/ui/src/lib/fire-calculator/fire-calculator.service.spec.ts index fbdc859d2..43ced5850 100644 --- a/libs/ui/src/lib/fire-calculator/fire-calculator.service.spec.ts +++ b/libs/ui/src/lib/fire-calculator/fire-calculator.service.spec.ts @@ -32,6 +32,38 @@ describe('FireCalculatorService', () => { 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 () => { const r = 0.05; const P = 100000; diff --git a/libs/ui/src/lib/fire-calculator/fire-calculator.service.ts b/libs/ui/src/lib/fire-calculator/fire-calculator.service.ts index 848a9efa4..8090e854c 100644 --- a/libs/ui/src/lib/fire-calculator/fire-calculator.service.ts +++ b/libs/ui/src/lib/fire-calculator/fire-calculator.service.ts @@ -52,7 +52,7 @@ export class FireCalculatorService { }) { if (r === 0) { // No compound interest - return (totalAmount - P) / PMT; + return Math.max(0, (totalAmount - P) / PMT); } else if (totalAmount <= P) { return 0; }