Percent profit is a crucial metric in business and finance, indicating the profitability of an investment or sale relative to its original cost. It answers the question: "How much did I gain, as a percentage of what I initially spent?" A positive percent profit signifies a gain, while a negative one indicates a loss.
The Formula
The calculation involves two key values: the Cost Price and the Selling Price.
First, you determine the absolute profit (or loss):
Profit = Selling Price – Cost Price
Next, you express this profit as a percentage of the original cost price. This is done using the following formula:
This means you made a 50% profit on your initial investment.
If the selling price was $80, the profit would be $80 – $100 = -$20 (a loss). The percent profit would be (-$20 / $100) * 100 = -20%, indicating a 20% loss.
function calculateProfit() {
var costPriceInput = document.getElementById("costPrice");
var sellingPriceInput = document.getElementById("sellingPrice");
var resultDiv = document.getElementById("result");
var costPrice = parseFloat(costPriceInput.value);
var sellingPrice = parseFloat(sellingPriceInput.value);
// Input validation
if (isNaN(costPrice) || isNaN(sellingPrice)) {
resultDiv.innerHTML = "Please enter valid numbers for both prices.";
resultDiv.style.color = "#dc3545"; // Red for error
resultDiv.style.borderColor = "#dc3545";
return;
}
if (costPrice = 0) {
resultDiv.innerHTML = "Percent Profit: " + formattedPercentProfit + "%";
resultDiv.style.color = "#004a99"; // Blue for profit
resultDiv.style.borderColor = "#28a745"; // Green border for success
} else {
resultDiv.innerHTML = "Percent Loss: " + formattedPercentProfit + "%";
resultDiv.style.color = "#dc3545"; // Red for loss
resultDiv.style.borderColor = "#dc3545"; // Red border for loss
}
}