diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e6752795..05e432d0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -270,6 +270,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 - Fixed an issue in the data provider service where quotes could be missing for symbols that exist in multiple data sources by keying the quotes response by the asset profile identifier 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; }