Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to calculate Amount by Number of days, as Considering year,month,weak,day but it does not return correct value

my code is ,Please suggest me correction if any

C#
double year = 0, month = 0, weak = 0, day= 0;
               if (delay > 365)
                    year = delay % 365;
               if (delay < 365 && delay >= 31)
                   month = delay % 30;
               if (delay < 30 && delay >= 7)
                   weak = delay % 7;
               if (delay < 7)
                   day = delay % 1;
                        
            
               double year1=0, month1=0, weak1=0, day1=0;

                 year1 = delay / 365;
                 month1 = year / 12;
                 weak1 = month / 7;
                 day1 = day / 1;

           double yearVal = 100;
           double monthVal = 50;
           double weakVal = 10;
           double dayVal = 1;

           double Addition = year1 * yearVal + month1 * monthVal + weak1 * weakVal + day1 * dayVal;
Posted
Comments
Ron Beyer 25-Jan-14 0:28am    
So you have a variable that is the number of days, and you want the number of years, months, weeks, and days from that?
SVT02 25-Jan-14 0:33am    
yes,delay variable is number of days and also want to calculate The addition for these days by given value
Ron Beyer 25-Jan-14 0:35am    
I really don't understand what "calculate the addition for these days by given value" means exactly. The purpose of your formula is not clear in your code.
SVT02 25-Jan-14 0:38am    
suppose if delay=400 so i want this in 1year-1month-0weak-5days manner and Addition for this should 155, but my code doesn't work properly
BillWoodruff 25-Jan-14 0:45am    
Consider if delay == 21: all the calculated parameters will be set to zero, except for 'year 1 which will be 0.057534246575342465, and the result will be 5.7534246575342465 ... is that what you want ?

What is your goal here ?

1 solution

You can calculate the number of days/months/years like this:

C#
int years, months, weeks, days;
years = months = weeks = days = 0;

while (delay > 365)
{ years++; delay -= 365; }

while (delay > 30)
{ months++; delay -= 30; }

while (delay > 7)
{ weeks++; delay -= 7; }

days = delay;

int yearMult = 100;
int monthMult = 50;
int weekMult = 10;
int dayMult = 1;

int addition = (years * yearMult) + (months * monthMult) + (weeks * weekMult) + (days * dayMult);

//So for 400 as the "delay" that should give you 1 year, 1 month, 0 weeks and 5 days, and "addition" will equal 155
 
Share this answer
 
v2
Comments
SVT02 25-Jan-14 0:43am    
But how can i multiply by given values with years,month,week, days?
Ron Beyer 25-Jan-14 0:46am    
I just updated it to show you. I ran a test with this and 400 as your example and addition produced a value of 155...
SVT02 25-Jan-14 0:48am    
Thanks Mr Ron,I will try it
SVT02 25-Jan-14 0:51am    
Thanks a lot Ron,Its Run Perfectly
BillWoodruff 25-Jan-14 2:00am    
Note that we do not have information what Type 'delay is: in the OP's code all variables declared are Type 'double. If 'delay is also declared as 'double, then the result of the computation will be a floating point number !

If 'delay is declared as 'int, then the OP could re-define his variables as 'int.

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