This calculator helps you determine the inflation rate between two periods based on the Consumer Price Index (CPI) or any price index. Inflation is the general increase in prices and fall in the purchasing value of money. Understanding inflation helps in economic planning, investment strategies, and understanding the erosion of purchasing power over time.
function calculateInflation() {
var initialIndex = parseFloat(document.getElementById("initialIndex").value);
var finalIndex = parseFloat(document.getElementById("finalIndex").value);
var initialYear = parseInt(document.getElementById("initialYear").value);
var finalYear = parseInt(document.getElementById("finalYear").value);
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
if (isNaN(initialIndex) || isNaN(finalIndex) || isNaN(initialYear) || isNaN(finalYear)) {
resultDiv.innerHTML = "Please enter valid numbers for all fields.";
return;
}
if (initialIndex <= 0 || finalIndex <= 0) {
resultDiv.innerHTML = "Price Index values must be positive.";
return;
}
if (finalYear <= initialYear) {
resultDiv.innerHTML = "Final Year must be greater than the Initial Year.";
return;
}
// Calculate the inflation rate using the formula:
// Inflation Rate = ((Final Index – Initial Index) / Initial Index) * 100
var inflationRate = ((finalIndex – initialIndex) / initialIndex) * 100;
// Calculate the average annual inflation rate
var numberOfYears = finalYear – initialYear;
var averageAnnualInflationRate = inflationRate / numberOfYears;
resultDiv.innerHTML =
"Calculation Details:" +
"Initial Price Index (Year " + initialYear + "): " + initialIndex.toFixed(2) + "" +
"Final Price Index (Year " + finalYear + "): " + finalIndex.toFixed(2) + "" +
"Total Inflation over " + numberOfYears + " year(s): " + inflationRate.toFixed(2) + "%" +
"Average Annual Inflation Rate: " + averageAnnualInflationRate.toFixed(2) + "%";
}
.calculator-wrapper {
font-family: sans-serif;
border: 1px solid #ddd;
padding: 20px;
border-radius: 8px;
max-width: 500px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-wrapper h2 {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.calculator-inputs {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
margin-bottom: 20px;
}
.input-group {
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.input-group input {
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1em;
}
.calculator-wrapper button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-wrapper button:hover {
background-color: #45a049;
}
.calculator-result {
margin-top: 25px;
padding: 15px;
border: 1px dashed #eee;
border-radius: 4px;
background-color: #fff;
text-align: center;
}
.calculator-result p {
margin-bottom: 10px;
font-size: 1.1em;
line-height: 1.5;
}
.calculator-result strong {
color: #333;
}