Jump to content

help with calculation

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
jellybean

jellybean

    Newbie

  • Members
  • Pip
  • 2 posts
Hallo can someone please help me, I've got this table:

Valuation Date Construction
30/03/2009 30,625.26
30/04/2009 269,825.04
30/05/2009 850,056.45
30/06/2009 1,922,475.06
30/07/2009 3,500,852.19
30/08/2009 5,757,055.16
30/09/2009 8,699,041.1
30/10/2009 12,232,330.17
30/11/2009 16,612,763.46
30/12/2009 19,166,542.19
30/01/2010 22,098,191.54
28/02/2010 33,539,289.47
30/03/2010 40,518,025
30/04/2010 48,372,377.98
30/05/2010 56,543,248.62
30/06/2010 65,507,314.28
30/07/2010 74,607,415.4
30/08/2010 84,352,063.34
30/09/2010 94,325,187.87
30/10/2010 104,060,459.19
30/11/2010 114,040,884.2
30/12/2010 119,094,482.8
30/01/2011 124,342,210.61
28/02/2011 140,316,539.09
30/03/2011 146,909,564.95

Calculated from this formula:


public string Calculation(double b, DateTime validation, DateTime start, DateTime end, double alpha, double beta)

    {

            //start date(t1) 

        System.TimeSpan sp_start =

            validation.Subtract(start);

        string sp_start_str = sp_start.TotalDays.ToString("#");

        double t1 = double.Parse(sp_start_str);


        bool changed = false;

        if (validation.Month == 12 || validation.Month == 01)

        {

            changed = true;

        }

            // change accordingly

        if (validation.Month == 12)

            t1 = t1 - 14;

        if (validation.Month == 01)

            t1 = t1 - 28;


            // end date(t2)

        System.TimeSpan sp_end =

            end.Subtract(start);

        string sp_end_str = sp_end.TotalDays.ToString("#");

        double t2 = double.Parse(sp_end_str);

        t2 = t2 - 28;


        double divide = (t1) / (t2);

        double minus1 = (1 - (divide));

        double pow1 = 1 - (Math.Pow(minus1, alpha));

        double calculation = Math.Round(b * (Math.Pow(pow1, beta)), 2);

        

        return String.Format("{0:#,###,###,###.##}", calculation);

    }


What i need to do is:
The first January, all the months following t1 = 28;
After the second January, all the months following t1 = 56;

I probably need to use a for loop??? But not sure how to go about this.
Please someone help!!

Thanks.

#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I guess This calculation function works only for one month. You are calling this calculation function for each and every month. So, You can't go for a for loop inside the function.

I can suggest a way. You can have global static variable named Jan_check or something like that
 public static int Jan_check = 0;

public string Calculation(double b, DateTime validation, DateTime start, DateTime end, double alpha, double beta)
    {
            //start date(t1) 
        System.TimeSpan sp_start =
            validation.Subtract(start);
        string sp_start_str = sp_start.TotalDays.ToString("#");
        double t1 = double.Parse(sp_start_str); // t1 = 28
        for(int i=0; i<Jan_check; i++)
             t1 = t1 + 28;

        bool changed = false;
        if (validation.Month == 12 || validation.Month == 01)
        {
            changed = true;
        }
            // change accordingly
        if (validation.Month == 12)
            t1 = t1 - 14;
        if (validation.Month == 01)
            t1 = t1 - 28;

            // end date(t2)
        System.TimeSpan sp_end =
            end.Subtract(start);
        string sp_end_str = sp_end.TotalDays.ToString("#");
        double t2 = double.Parse(sp_end_str);
        t2 = t2 - 28;

        double divide = (t1) / (t2);
        double minus1 = (1 - (divide));
        double pow1 = 1 - (Math.Pow(minus1, alpha));
        double calculation = Math.Round(b * (Math.Pow(pow1, beta)), 2);
        
        // at the end of every year, increase the Jan_check
        if(validation.month == 12)
        {
                 Jan_check++;
        }     
        return String.Format("{0:#,###,###,###.##}", calculation);
    }


I hope this should work....