function calculateROI() {
// Get input values using var
var initialInput = document.getElementById('initialInvest');
var finalInput = document.getElementById('finalValue');
var timeInput = document.getElementById('investmentDuration');
var resultContainer = document.getElementById('roiResultOutput');
var initialAmount = parseFloat(initialInput.value);
var finalAmount = parseFloat(finalInput.value);
var years = parseFloat(timeInput.value);
// Validation
if (isNaN(initialAmount) || isNaN(finalAmount)) {
alert("Please enter valid numbers for the Investment Amount and Final Value.");
return;
}
if (initialAmount === 0) {
alert("Initial investment cannot be zero.");
return;
}
// Calculate Net Profit
var netProfit = finalAmount – initialAmount;
// Calculate Total ROI Formula: ((Final Value – Initial Value) / Initial Value) * 100
var totalROI = (netProfit / initialAmount) * 100;
// Calculate Annualized ROI if time is provided
// Formula: ((Final Value / Initial Value) ^ (1 / Years)) – 1
var annualizedROI = 0;
var hasTime = !isNaN(years) && years > 0;
if (hasTime) {
if (initialAmount > 0 && finalAmount >= 0) {
var growthFactor = finalAmount / initialAmount;
annualizedROI = (Math.pow(growthFactor, 1 / years) – 1) * 100;
} else {
// CAGR calculation is complex for negative start/end, default to 0 or hide
annualizedROI = 0;
}
}
// Update UI
resultContainer.style.display = 'block';
// Format Profit
var profitElement = document.getElementById('netProfitDisplay');
profitElement.innerHTML = "$" + netProfit.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
if (netProfit < 0) {
profitElement.className = "roi-result-value roi-negative";
} else {
profitElement.className = "roi-result-value roi-highlight";
}
// Format Total ROI
var totalRoiElement = document.getElementById('totalRoiDisplay');
totalRoiElement.innerHTML = totalROI.toFixed(2) + "%";
if (totalROI < 0) {
totalRoiElement.className = "roi-result-value roi-negative";
} else {
totalRoiElement.className = "roi-result-value roi-highlight";
}
// Format Annualized ROI
var annualizedContainer = document.getElementById('annualizedContainer');
if (hasTime) {
annualizedContainer.style.display = 'flex';
var annRoiElement = document.getElementById('annualizedRoiDisplay');
annRoiElement.innerHTML = annualizedROI.toFixed(2) + "%";
if (annualizedROI < 0) {
annRoiElement.className = "roi-result-value roi-negative";
} else {
annRoiElement.className = "roi-result-value roi-highlight";
}
} else {
annualizedContainer.style.display = 'none';
}
}
Formula to Calculate Rate of Return on Investment
Understanding the performance of your assets is fundamental to financial success. The Rate of Return on Investment (ROI) is a key performance indicator (KPI) used to evaluate the efficiency or profitability of an investment or to compare the efficiency of a number of different investments.
Our calculator above uses the standard financial formulas to help you instantly determine your percentage gain or loss. Below, we break down the math so you can understand exactly how these figures are derived.
The Basic ROI Formula
The standard formula to calculate the rate of return on investment is straightforward. It measures the net profit relative to the cost of the investment.
ROI = ( (Final Value – Initial Cost) / Initial Cost ) × 100
Where:
Final Value: The current value of the investment or the amount you sold it for.
Initial Cost: The original amount you paid to acquire the asset.
Net Profit: The difference between the Final Value and the Initial Cost.
Example Calculation
Let's say you purchased stock for $1,000 (Initial Cost) and sold it two years later for $1,250 (Final Value).
First, calculate the net profit: $1,250 – $1,000 = $250.
Next, divide the profit by the initial cost: $250 / $1,000 = 0.25.
Finally, multiply by 100 to get the percentage: 0.25 × 100 = 25%.
Your total Rate of Return is 25%.
Total ROI vs. Annualized ROI
While the standard formula gives you the total return, it does not account for the passage of time. A 25% return over 1 year is fantastic, but a 25% return over 10 years is less impressive due to inflation and opportunity cost.
To solve this, we use the Annualized ROI formula (often referred to as Compound Annual Growth Rate or CAGR), which is also calculated by our tool if you enter the investment duration.
Annualized ROI = [ (Final Value / Initial Cost) ^ (1 / Years) ] – 1
Why Tracking ROI Matters
Investors use this metric to evaluate portfolios, making it easier to decide whether to hold or sell an asset. It applies to various asset classes:
Asset Class
Typical ROI Factors
Stocks
Price appreciation + Dividends
Real Estate
Property value increase + Rental income – Maintenance costs
Business
Net profit margins / Capital invested
Limitations of the ROI Formula
While powerful, the basic ROI calculation has limitations. It does not consider risk tolerance, transaction fees (like brokerage commissions), or taxes unless you manually deduct these from your "Final Value." Furthermore, simply looking at the percentage doesn't tell you about the liquidity of the asset—how easy it is to convert back into cash.
Always consider the time horizon (calculated via our annualized feature) and the risk profile of the investment alongside the raw percentage return.