Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi..

In my application I have implemented EMI calculator..
Decimal datatype is used throughout the calculation..
Here I am not getting the full decimal value..So the Total value getting mismatch..
I have replaced the type with long,double….Still not getting…
Please give me a good solution..
Thanks in advance

This is my code [Edit: Moved from OP's comment to question]
C#
Decimal Interest = 0;
EMICalculatorList ECL = new EMICalculatorList();
ProjectXBL.Config.EMICalculator EC;
int month = ddlMonth.SelectedValue.ToNonNullInt32() - 1;
Decimal Intr = 0; Decimal Mth = txtPrinciple.Text.ToNonNullDecimal() / txtLoanDurationInMonths.Text.ToNonNullDecimal();
Decimal principalAmt = txtPrinciple.Text.ToNonNullDecimal();
Decimal ROI = txtAnnualInterestRate.Text.ToNonNullDecimal();
Decimal months = txtLoanDurationInMonths.Text.ToNonNullDecimal();
Decimal F = 1200;
for (int i = 0; i < txtLoanDurationInMonths.Text.ToNonNullInt32(); i++)
{
    Intr = Intr + ((principalAmt * ROI) / F);
    principalAmt = principalAmt - Mth;
}
Decimal Avg = Intr.ToNonNullDecimal() / txtLoanDurationInMonths.Text.ToNonNullDecimal();
Decimal MonthlyPayment = ((txtPrinciple.Text.ToNonNullDecimal() / txtLoanDurationInMonths.Text.ToNonNullDecimal()) + Avg).ToNonNullDecimal();
lblMonthlypaymentsText.Text = string.Empty;
lblMonthlypaymentsText.Text = Math.Round(MonthlyPayment, 2).ToString();
lblAnnulLoanPaymentsText.Text = string.Empty;
lblAnnulLoanPaymentsText.Text = Math.Round((MonthlyPayment * 12), 2).ToString();
Decimal Ending = 0;
Decimal Ints = 0;
Decimal Prnc = 0;
Decimal CumPrinc = 0;
Decimal CumIntr = 0;
Decimal principal = txtPrinciple.Text.ToNonNullDecimal();
for (int i = 0; i < txtLoanDurationInMonths.Text.ToNonNullInt32(); i++)
{
    EC = new ProjectXBL.Config.EMICalculator();
    //No,Month And Year
    EC.No = i + 1;
    if (month == 0)
    {
        EC.Year = txtYear.Text;
    }
    if (month == 12)
    {
        month = 0;
        EC.Year = (txtYear.Text.ToNonNullInt32() + 1).ToString();
    }
    EC.Month = ddlMonth.Items[month].Text; month++;
    //
    EC.Payment = MonthlyPayment;
    Decimal monthly = principal / months;
    //Monthly payable without interest
    EC.Interest = (principal * ROI) / 1200;
    Ints = EC.Interest;
    principal = principal - monthly;
    Interest = Interest + EC.Interest;
    EC.Principal = MonthlyPayment - EC.Interest;
    Prnc = EC.Principal;
    if (i == 0)
    {
        EC.BeginningBalance = txtPrinciple.Text.ToNonNullDecimal();
        EC.CumulativePrinciple = EC.Principal;
        EC.CumulativeInterest = EC.Interest;
    }
    else
    {
        EC.BeginningBalance = Ending;
        EC.CumulativePrinciple = CumPrinc + Prnc;
        EC.CumulativeInterest = CumIntr + Intr;
    }
    EC.Ending = EC.BeginningBalance - EC.Principal;
    Ending = EC.Ending;
    CumPrinc = EC.CumulativePrinciple;
    CumIntr = EC.CumulativeInterest;
    ECL.Add(EC);
}
lstEMI.DataSource = ECL;
lstEMI.DataBind();
Posted
Updated 1-Jun-11 0:36am
v3
Comments
Toniyo Jackson 1-Jun-11 6:03am    
Share your code.
Deepthi Aravind 1-Jun-11 6:07am    
This is my code
<pre>
Decimal Interest = 0;
EMICalculatorList ECL = new EMICalculatorList();
ProjectXBL.Config.EMICalculator EC;
int month = ddlMonth.SelectedValue.ToNonNullInt32() - 1;
Decimal Intr = 0;
Decimal Mth = txtPrinciple.Text.ToNonNullDecimal() / txtLoanDurationInMonths.Text.ToNonNullDecimal();
Decimal principalAmt = txtPrinciple.Text.ToNonNullDecimal();
Decimal ROI = txtAnnualInterestRate.Text.ToNonNullDecimal();
Decimal months = txtLoanDurationInMonths.Text.ToNonNullDecimal();
Decimal F = 1200;
for (int i = 0; i < txtLoanDurationInMonths.Text.ToNonNullInt32(); i++)
{
Intr = Intr + ((principalAmt * ROI) / F);
principalAmt = principalAmt - Mth;
}
Decimal Avg = Intr.ToNonNullDecimal() / txtLoanDurationInMonths.Text.ToNonNullDecimal();
Decimal MonthlyPayment = ((txtPrinciple.Text.ToNonNullDecimal() / txtLoanDurationInMonths.Text.ToNonNullDecimal()) + Avg).ToNonNullDecimal();
lblMonthlypaymentsText.Text = string.Empty;
lblMonthlypaymentsText.Text = Math.Round(MonthlyPayment, 2).ToString();
lblAnnulLoanPaymentsText.Text = string.Empty;
lblAnnulLoanPaymentsText.Text = Math.Round((MonthlyPayment * 12), 2).ToString();
Decimal Ending = 0;
Decimal Ints = 0;
Decimal Prnc = 0;
Decimal CumPrinc = 0;
Decimal CumIntr = 0;
Decimal principal = txtPrinciple.Text.ToNonNullDecimal();
for (int i = 0; i < txtLoanDurationInMonths.Text.ToNonNullInt32(); i++)
{
EC = new ProjectXBL.Config.EMICalculator();
//No,Month And Year
EC.No = i + 1;
if (month == 0)
{
EC.Year = txtYear.Text;
}
if (month == 12)
{
month = 0;
EC.Year = (txtYear.Text.ToNonNullInt32() + 1).ToString();
}
EC.Month = ddlMonth.Items[month].Text;
month++;
//
EC.Payment = MonthlyPayment;

Decimal monthly = principal / months;//Monthly payable without interest
EC.Interest = (principal * ROI) / 1200;
Ints = EC.Interest;
principal = principal - monthly;
Interest = Interest + EC.Interest;
EC.Principal = MonthlyPayment - EC.Interest;
Prnc = EC.Principal;
if (i == 0)
{
EC.BeginningBalance = txtPrinciple.Text.ToNonNullDecimal();
EC.CumulativePrinciple = EC.Principal;
EC.CumulativeInterest = EC.Interest;
}
else
{
EC.BeginningBalance = Ending;
EC.CumulativePrinciple = CumPrinc + Prnc;
EC.CumulativeInterest = CumIntr + Intr;
}
EC.Ending = EC.BeginningBalance - EC.Principal;
Ending = EC.Ending;
CumPrinc = EC.CumulativePrinciple;
CumIntr = EC.CumulativeInterest;
ECL.Add(EC);
}
lstEMI.DataSource = ECL;
lstEMI.DataBind();
</pre>
Deepthi Aravind 2-Jun-11 2:11am    
I solved this issue using javascript....

1 solution

Try Using the BigInt. It can hold virtually infinite number. it is introduced with 4.0 and in the System.Numeric namespace
 
Share this answer
 
Comments
Deepthi Aravind 2-Jun-11 1:41am    
Thanks for your reply..I am using 3.5...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900