Small, dependency-free extension methods for simple and compound interest calculations.
dotnet add package InterestExtensionsTargets .NET Standard 2.0 and .NET 8. Tested on .NET 8 and .NET 10.
principalis a non-negative decimal amount.interestRateis a non-negative nominal annual rate expressed as a fraction: use0.05mfor 5% per year.periodis a non-negative number of whole years.compoundingPeriodCountis the total number of times interest is applied.compoundingPeriodsPerYearis a positive frequency used to derive the periodic rate.Yearly,Monthly, andDailycompound 1, 12, and 365 times per year respectively.- Daily compounding uses a fixed 365-day year; it is not a date-based day-count convention.
- Results are returned without implicit rounding. Choose the scale and midpoint rule required by your currency and domain.
- Invalid inputs throw
ArgumentOutOfRangeException; calculations outside the range ofdecimalthrowOverflowException.
The formulas are:
simple total = principal × (1 + annual rate × years)
periodic rate = annual rate / periods per year
compound total = principal × (1 + periodic rate)^period count
using InterestExtension;
using InterestExtension.Enums;
decimal principal = 1_000m;
decimal annualRate = 0.05m; // 5% per year
int years = 3;
decimal simpleTotal = principal.CalculateSimpleInterest(annualRate, years);
decimal simpleInterest = principal.CalculateSimpleInterestAmount(annualRate, years);
decimal annualTotal = principal.CalculateCompoundInterest(annualRate, years);
decimal annualInterest = principal.CalculateCompoundInterestAmount(annualRate, years);
decimal monthlyTotal = principal.CalculateCompoundInterest(
annualRate,
years,
InterestPeriod.Monthly);
// 18 monthly compounding periods, equivalent to 18 months.
decimal eighteenMonthTotal = principal.CalculateCompoundInterestForPeriods(
annualRate,
compoundingPeriodCount: 18,
compoundingPeriodsPerYear: 12);
decimal eighteenMonthInterest = principal.CalculateCompoundInterestAmountForPeriods(
annualRate,
compoundingPeriodCount: 18,
compoundingPeriodsPerYear: 12);
// InterestExtensions does not choose a financial rounding policy for you.
decimal displayAmount = decimal.Round(monthlyTotal, 2, MidpointRounding.ToEven);| Method | Result |
|---|---|
CalculateSimpleInterest |
Principal plus simple interest |
CalculateSimpleInterestAmount |
Simple interest only |
CalculateCompoundInterest |
Principal plus compound interest for whole years |
CalculateCompoundInterestAmount |
Compound interest only for whole years |
CalculateCompoundInterestForPeriods |
Principal plus compound interest for a custom period count and frequency |
CalculateCompoundInterestAmountForPeriods |
Compound interest only for a custom period count and frequency |
InterestExtensions provides deterministic simple and compound interest calculations using decimal. Date-based calculations and product-specific financial rules are intentionally out of scope.
Contributions are welcome. See the contribution guide.