/// <summary> /// The interest accrued on the loan (calculated based on the number of days from the <see cref="PrincipalEffectiveDate"/>) /// </summary> /// <param name="asOf">The date to calculate accrued interest for</param> /// <returns>The interest accrued on the loan as of the provided date.</returns> /// <exception cref="InvalidOperationException">When the provided <paramref name="asOf"/> date is before the loan's <see cref="PrincipalEffectiveDate"/>.</exception> public decimal CalculateInterest(DateTime asOf) { if (asOf < PrincipalEffectiveDate) { throw new InvalidOperationException(); } var total = 0.00m; for (var i = PrincipalEffectiveDate.Year; i <= asOf.Year; i++) { if (i == PrincipalEffectiveDate.Year) { var dateDiff = 0; if (PrincipalEffectiveDate.Year == asOf.Year) { dateDiff = (asOf - PrincipalEffectiveDate).Days; } else { dateDiff = (new DateTime(PrincipalEffectiveDate.Year, 12, 31) - PrincipalEffectiveDate).Days; } total += ((Principal * InterestRate) / (decimal)PrincipalEffectiveDate.DaysInYear()) * dateDiff; continue; } if (i == asOf.Year) { var dateDiff = (asOf - new DateTime(asOf.Year, 1, 1)).Days; total += ((Principal * InterestRate) / (decimal)asOf.DaysInYear()) * dateDiff; continue; } var daysInYear = new DateTime(i, 1, 1).DaysInYear(); total += (Principal * InterestRate); } return(total); }
/// <summary> /// Calcuates the amount of interest accruing daily on the loan based on the number of days in the year. /// </summary> /// <returns></returns> public decimal CurrentDailyInterestRate() { return((Principal * InterestRate) / (decimal)PrincipalEffectiveDate.DaysInYear()); }