FUNCTION
yearFraction
Computes the fraction of the year between two serial dates, <span align="right" style="background-color:99FF99"><strong>Excel: YEARFRAC</strong></span>
You're viewing an older version of this page (#189). View the current version.
Interface
#include <codecogs/finance/banking/yearfraction.h>
using namespace Finance::Banking;
Returns a number between 0 and 1 (normally), which represents the fraction of the year between two serial Julian dates:
If there is more than 1 year between the dates, then this value will be greater than 1.
The European or USA 360-day accounting system (with 30 days months - see dateDiff360), applies the following transformation to the start and end dates. \copydoc constants dateDiffType
Example 1
#include <stdio.h>
#include <codecogs/units/date/date.h>
#include <codecogs/units/date/yearfraction.h>
#include <codecogs/units/date/isleapyear.h>
using namespace Units::Date;
int main()
{
printf("\n StartDate EndDate Basis: 0 (USA) 1 2 3 4 (European)");
int adate=date("25 Dec 2002");
for(int i=0;i<200;i+=20)
{
int d,m,y;
dateYMD(adate, y, m, d);
printf("\n %2d-%2d-%d", d,m,y);
dateYMD(adate+i, y, m, d);
printf(" %2d-%2d-%d", d,m,y);
printf(" %5.3lf %5.3lf %5.3lf %5.3lf %5.3lf",
yearFraction(adate, adate+i, 0), // USA
yearFraction(adate, adate+i, 1), // Actual/Actual
yearFraction(adate, adate+i, 2), // Actual/360
yearFraction(adate, adate+i, 3), // Actual/365
yearFraction(adate, adate+i, 4)); // European
}
return 0;
}Output:
StartDate EndDate Basis: 0 (USA) 1 2 3 4 (European)
25-12-2003 25-12-2003 0.000 0.000 0.000 0.000 0.000
25-12-2003 14- 1-2004 0.053 0.055 0.056 0.055 0.053
25-12-2003 3- 2-2004 0.106 0.110 0.111 0.110 0.106
25-12-2003 23- 2-2004 0.161 0.164 0.167 0.164 0.161
25-12-2003 14- 3-2004 0.219 0.219 0.222 0.219 0.219
25-12-2003 3- 4-2004 0.272 0.273 0.278 0.274 0.272
25-12-2003 23- 4-2004 0.328 0.328 0.333 0.329 0.328
25-12-2003 13- 5-2004 0.383 0.383 0.389 0.384 0.383
25-12-2003 2- 6-2004 0.436 0.437 0.444 0.438 0.436
25-12-2003 22- 6-2004 0.492 0.492 0.500 0.493 0.492Parameters
<table border="0"> <tr><td width="15px"> </td><td width="80px" valign="top">0 (default)</td><td><em>USA</em> (NASAD) 30 day accounting month lengths over a 360 day year length.</td></tr> <tr><td></td><td valign="top">1 </td><td>Actual date difference over the actual year length (365 or 366 in a leap year). The year length is defined by the StartDate year.</td></tr> <tr><td></td><td valign="top">2 </td><td>Actual date difference over a 360 day year.</td></tr> <tr><td></td><td valign="top">3 </td><td>Actual date difference over a 365 day year.</td></tr> <tr><td></td><td valign="top">4 </td><td><em>European</em> 30 day accounting month lengths over a 360 day year length.</td></tr> </table>
Interactive Calculator
Computing…
Set a range above first to export a graph.