GuardianZ
GuardianZ
CC#
Created by GuardianZ on 3/12/2024 in #help
Unable to calculate/store correct value to the db
Hello. I am developing a mortgage tracker for myself and currently trying to calculate my daily interest in my service controller. I am capturing my current interest rate and outstanding balance using the front end form, then the idea is to retrieve this value from the database in my service, calculate my actual daily interest rate and store it back into the db in my main interest rate controller. When I open my SQL explorer the value is unfortunately stored as 0.0000 decimal value and I can't debug it for the world at the moment as to why? This is the service controller:
csharp
namespace Finance101.Services
{
public class DailyInterestRateService
{

private readonly Finance101Context _context;

public DailyInterestRateService()
{
}

public DailyInterestRateService(Finance101Context context)
{
_context = context;
}

public async Task<decimal> CalculateDailyInterestRate()
{
int daysInYear = 365;
decimal dailyInterestRate;
//retrieve current interest rate from the database
var mortgageForm = await _context.MortgageForm.FirstOrDefaultAsync();
if (mortgageForm == null)
{
throw new Exception("No mortgage form data found in the database");
}
else
{
dailyInterestRate = (decimal)mortgageForm.CurrentInterestRate * mortgageForm.OutstandingMortgageBalance / (100 * daysInYear);
}

return dailyInterestRate;
}
}
}
csharp
namespace Finance101.Services
{
public class DailyInterestRateService
{

private readonly Finance101Context _context;

public DailyInterestRateService()
{
}

public DailyInterestRateService(Finance101Context context)
{
_context = context;
}

public async Task<decimal> CalculateDailyInterestRate()
{
int daysInYear = 365;
decimal dailyInterestRate;
//retrieve current interest rate from the database
var mortgageForm = await _context.MortgageForm.FirstOrDefaultAsync();
if (mortgageForm == null)
{
throw new Exception("No mortgage form data found in the database");
}
else
{
dailyInterestRate = (decimal)mortgageForm.CurrentInterestRate * mortgageForm.OutstandingMortgageBalance / (100 * daysInYear);
}

return dailyInterestRate;
}
}
}
4 replies