0

I have a foreach loop in the code-behind file which is meant to evaluate and calculate for each item shown in a grid and update a total fee on the page. The problem is that the calculation is only returning part of the answer. The part it returns is correct, but it is not performing the full calculation. I believe it is failing when it finds the first condition.

The business logic is this: If an asset has a monthly or total cost less than or equal to $2000, then the cost of that asset is $1007.95 period, regardless of how many months it is checked out (refer to the duration spinner in the grid).

If an asset has a monthly or total cost greater than $2000, then the cost of that asset is its cost multiplied by the number of months (duration) it is checked out.

I have inherited this bug & project, so I have included the full code for the foreach, and supplied two supporting screenshots below (I have had to obfuscate some of the information in the screenshots but relevant elements are shown).

The Code:

    private void PopulateFormFees(CheckOut c)
    {
        double dblTotalMonthlyFee = 0;
        double dblTotalCharge = 0; 

        c.CheckOutAssets.ForEach(delegate(CheckOutAsset coa)
        {
            if (coa.UsageFee < 2001)
            {
                dblTotalMonthlyFee = 1007.95;
                dblTotalCharge = 1007.95 * 1;
            }

            if (coa.UsageFee > 2000)
            {
                dblTotalMonthlyFee += coa.UsageFee;
                dblTotalCharge += (coa.UsageFee * coa.DurationMonths);
            }
        });

Screencaptures:

Screenshot 1 displays the form data on initial view. Here we can see that the Total Charge label is displaying $5,132.95 which indicates that the foreach loop is calculating for the $4,125.00 fee and one $1,007.95 fee, but not the others.

Total fee bug example 1

Screenshot 2 displays an example where duration was increased for each item. Here we see that the Total Charge label is displaying $21,632.95 which indicates that the foreach loop is calculating correctly for the $4,125.00 fee and for one $1,007.95 fee, but not the others.

Total fee bug example 2

Thank you for your consideration and taking a look.

6
  • 4
    Never, ever, ever use doubles for financial, or precise calculations. Use decimal instead. exploringbinary.com/… Commented Aug 6, 2014 at 13:41
  • Have you debugged your code and inspected the values involved? The reason I ask is because you provide quite the comprehensive question here, perhaps too comprehensive, for us to do the debugging for you. Commented Aug 6, 2014 at 13:43
  • 1
    @Dave Understood, but refactoring & optimization will come later. As I mentioned, I inherited this project and I need to get these bugs worked out first. Thank you for your insight, it's on my todo list. Commented Aug 6, 2014 at 13:44
  • You have overlap from 2000.01 to 2000.99 these would be counted twice. Commented Aug 6, 2014 at 13:46
  • @Peter Thank you for your response. I hesitate to go too deeply here but the environment I just got tossed into is so, how can I say this, not optimal...the security is convoluted and insane. So much so that I cannot build locally to debug at the moment. They are supposed to be fixing that at the moment, but of course this bug was given a "10 minutes ago" priority. Commented Aug 6, 2014 at 13:46

2 Answers 2

7

This just overwrites whatever values were stored in these variables before:

dblTotalMonthlyFee = 1007.95;
dblTotalCharge = 1007.95 * 1;

Correct way is to add to current value, as in the other branch of the if:

dblTotalMonthlyFee += 1007.95;
dblTotalCharge += 1007.95 * 1;
Sign up to request clarification or add additional context in comments.

Comments

2

Change

dblTotalMonthlyFee = 1007.95;
dblTotalCharge = 1007.95 * 1;

to

dblTotalMonthlyFee += 1007.95;
dblTotalCharge += 1007.95 * 1;

and it should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.