Link to home
Start Free TrialLog in
Avatar of Greengiants15
Greengiants15

asked on

How to prorate a yearly subscription

Hello Experts!

I'm creating a subscription-based calculator in Java and I'm trying to figure out how to correctly prorate the value if the subscription.
My yearly subscription fee is a flat $120 per year (365 days).  If a user wants only a 6 month subscription then the cost is $60.  But let's say the subscriber signs on Jan. 15, 2011 and ends his subscription on June 30.  That's only 5 months and 15 days.  I need a formula that can correctly calculate the amount I need to charge.  In this case it would be ~$55.00.  It gets a little trickier if my user signs on Feb. 6th and ends their subscription Dec. 28.
Can anyone help?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why not calculate per day?
The formula you are looking for is -

120/365 * No of days

As CEHJ has pointed out it is easier to calc the daily amount, then multiply by the number of days.
Obviously the amount per day is fuzzy due to leap years, so one month in July could cost just fractionally less on a leap year.

days in year = 1/1/next - 1/1/current
days subscribed = last - first + 1
amount to pay = days subscribed / days in year * $120
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I stress again that you need the +1 in

last - first + 1

Otherwise, you will never get the full year in 1/1 - 31/12, you will be short 1 day
Not only that, a subscription for 7/7-7/7 will result in 0 days, when common sense says 1?
Avatar of Greengiants15
Greengiants15

ASKER

All - thank so so much for your solutions.  I do have one caveat.  What if a user starts their subscription on Feb.15, 2010 and ends their subscription March 15.  Really, I should charge the user $10 since it's one month but with the formula given:
days * PER_YEAR / 365
That would be:
29 *120 / 365 = 9.53

I guess for situations like this I would have to write a separate condition to calculate something like this?
then you want to count the # months instead of # days

objects - so are you saying that if I have a situation like this:
Start date: 2/15/2010
End date: 3/30/2010

I first have to calculate the number of months then calculate the number of days?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what you have to do depends on how you want to charge :)
I want to charge $10 for a month, 120 for a year and prorate the days if it falls over a month.
Is there a way to calculate the number of months?
Ex:
1/15/2010
to
6/15/2011

=

5 months
Using months is not going to help you. They have different numbers of days
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial