Possible Duplicate:
Proportionately distribute (prorate) a value across a set of values
I have been looking for an algorithm to distibute the total amount monthly based on the number of days of the year in C# so that the sum of all proportons is equal to the total value.
A date range within a year, and total amount is given.
Example:
for a value 19550 and date range 9/1/2011 to 6/30/2012
September 1,929.28
October 1,993.59
November 1,929.28
December 1,993.59
January 1,993.59
February 1,864.97 - 28 days
March 1,993.59 - 31 days
April 1,929.28 - 30 days
May 1,993.59
June 1,929.28
but the total is 19,550.04 which is .04 more than the total.
Along with Ricky’s answer:
It is not always possible to evenly distribute a given amount over a certain time period. Actually, it is quite rare that an amount is evenly distributed over a given time period.
This is generally accounted for in finance by allowing the last number be less than the average normal number in order to make up the difference.
For example, in your situation the amount for june should be 1929.24.
Generally you keep track of the amount assignment as you are building out the chart. Once you get to the end you have a choice. Either allow the amount to be higher or do one more payment for a much lower amount.
Basically, you need to change your algorithm that generates monthly amounts. Put a check in there to ensure that the amount you calculated is still available. If it’s not take the lower of the two values.