Customer Retention Rate Calculator
function calculateRetentionRate() {
var customersAtStart = parseFloat(document.getElementById("customersAtStart").value);
var customersAtEnd = parseFloat(document.getElementById("customersAtEnd").value);
var newCustomers = parseFloat(document.getElementById("newCustomers").value);
var resultDiv = document.getElementById("retentionRateResult");
if (isNaN(customersAtStart) || isNaN(customersAtEnd) || isNaN(newCustomers) || customersAtStart <= 0) {
resultDiv.innerHTML = "Please enter valid positive numbers for all fields. The number of customers at the start of the period must be greater than zero.";
return;
}
// Calculate retained customers: Customers at End – New Customers
var retainedCustomers = customersAtEnd – newCustomers;
// Prevent negative retained customers if new customers exceed end customers (which might indicate an error in data entry or a very specific scenario, but for standard CRR calculation, this shouldn't be negative)
if (retainedCustomers < 0) {
retainedCustomers = 0; // Or display an error/warning if appropriate for the business context
}
// Calculate retention rate
var retentionRate = (retainedCustomers / customersAtStart) * 100;
if (isNaN(retentionRate)) {
resultDiv.innerHTML = "Calculation error. Please check your inputs.";
} else {
resultDiv.innerHTML = "Your Customer Retention Rate is: " + retentionRate.toFixed(2) + "%";
}
}
.calculator-widget {
font-family: sans-serif;
border: 1px solid #ccc;
padding: 20px;
border-radius: 8px;
max-width: 600px;
margin: 20px auto;
background-color: #f9f9f9;
}
.calculator-title {
text-align: center;
margin-bottom: 20px;
color: #333;
}
.calculator-inputs {
display: grid;
grid-template-columns: 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[type="number"] {
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 1rem;
}
.calculate-button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 1.1rem;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculate-button:hover {
background-color: #0056b3;
}
.calculator-results {
margin-top: 25px;
padding-top: 15px;
border-top: 1px solid #eee;
text-align: center;
}
.calculator-results h3 {
margin-bottom: 10px;
color: #333;
}
#retentionRateResult {
font-size: 1.3rem;
color: #28a745;
font-weight: bold;
min-height: 30px; /* To prevent layout shifts */
}
.calculator-explanation {
margin-top: 30px;
border-top: 1px solid #eee;
padding-top: 20px;
font-size: 0.95rem;
line-height: 1.6;
color: #444;
}
.calculator-explanation h3 {
color: #333;
margin-bottom: 10px;
}
.calculator-explanation ul {
margin-left: 20px;
margin-bottom: 15px;
}
.calculator-explanation li {
margin-bottom: 5px;
}