Skip to content

Instantly share code, notes, and snippets.

@sterlu
Created February 24, 2020 14:54
Show Gist options
  • Save sterlu/4b44f59ea665819974ae684d7f564d9b to your computer and use it in GitHub Desktop.
Save sterlu/4b44f59ea665819974ae684d7f564d9b to your computer and use it in GitHub Desktop.
Convert between APR & APY yearly returns
const SECONDS_PER_YEAR = 365.25 * 24 * 60 * 60;
const BLOCKS_IN_A_YEAR = SECONDS_PER_YEAR / 14;
/**
* Formula source: http://www.linked8.com/blog/158-apy-to-apr-and-apr-to-apy-calculation-methodologies
*
* @param interest {Number} APY as percentage (ie. 6)
* @param frequency {Number} Compounding frequency (times a year)
* @returns {Number} APR as percentage (ie. 5.82 for APY of 6%)
*/
const apyToApr = (interest, frequency = BLOCKS_IN_A_YEAR) => ((1 + (interest / 100)) ** (1 / frequency) - 1) * frequency * 100;
/**
* Formula source: http://www.linked8.com/blog/158-apy-to-apr-and-apr-to-apy-calculation-methodologies
*
* @param interest {Number} APR as percentage (ie. 5.82)
* @param frequency {Number} Compounding frequency (times a year)
* @returns {Number} APY as percentage (ie. 6 for APR of 5.82%)
*/
const aprToApy = (interest, frequency = BLOCKS_IN_A_YEAR) => ((1 + (interest / 100) / frequency) ** frequency - 1) * 100;
Copy link

ghost commented May 3, 2023

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment