This calculator helps you understand the Annual Percentage Yield (APY) based on a given nominal interest rate and compounding frequency. APY is the real rate of return earned in a year, taking into account the effect of compounding interest.
function calculateAPY() {
var nominalRateInput = document.getElementById("nominalRate").value;
var compoundingPeriodsInput = document.getElementById("compoundingPeriods").value;
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
// Validate inputs
var nominalRate = parseFloat(nominalRateInput);
var compoundingPeriods = parseInt(compoundingPeriodsInput);
if (isNaN(nominalRate) || isNaN(compoundingPeriods) || nominalRate < 0 || compoundingPeriods <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields.";
return;
}
// Convert nominal rate from percentage to decimal
var rateAsDecimal = nominalRate / 100;
// Calculate APY formula: APY = (1 + r/n)^n – 1
// where r is the nominal annual rate (as a decimal) and n is the number of compounding periods per year.
var apy = Math.pow((1 + rateAsDecimal / compoundingPeriods), compoundingPeriods) – 1;
// Convert APY back to percentage and format
var formattedAPY = (apy * 100).toFixed(4);
resultDiv.innerHTML = "Nominal Annual Rate: " + nominalRate.toFixed(2) + "%" +
"Compounding Periods per Year: " + compoundingPeriods + "" +
"Calculated APY: " + formattedAPY + "%";
}
.calculator-container {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 15px;
}
.calculator-container p {
color: #555;
line-height: 1.6;
}
.input-group {
margin-bottom: 15px;
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #444;
}
.input-group input[type="number"] {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
box-sizing: border-box; /* Ensures padding and border are included in the element's total width and height */
}
.calculator-container button {
background-color: #007bff;
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
width: 100%;
transition: background-color 0.3s ease;
}
.calculator-container button:hover {
background-color: #0056b3;
}
.result-container {
margin-top: 20px;
padding: 15px;
border: 1px solid #ddd;
border-radius: 5px;
background-color: #eef7ff;
text-align: center;
}
.result-container p {
margin: 8px 0;
color: #333;
}
.result-container strong {
color: #007bff;
}