diff --git a/CHANGELOG.md b/CHANGELOG.md index e8d85de3e8..2df320be29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1683,6 +1683,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 fbdc859d22..43ced5850d 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 482aee2d57..08a4fe4b63 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; }