Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Maths/ReturnOnInvestment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Calculates Return on Investment (ROI) as a percentage.
* ROI measures the profitability of an investment relative to its cost.
*
* Formula: ROI = (Gain - Cost) / Cost * 100
*
* @see https://www.investopedia.com/terms/r/returnoninvestment.asp
*
* @param float $gainFromInvestment total value gained from the investment
* @param float $costOfInvestment total cost of the investment
* @return float ROI as a percentage
* @throws \InvalidArgumentException if costOfInvestment is not positive
*/
function returnOnInvestment(float $gainFromInvestment, float $costOfInvestment): float
{
if ($costOfInvestment <= 0) {
throw new \InvalidArgumentException('costOfInvestment must be greater than 0');
}
return ($gainFromInvestment - $costOfInvestment) / $costOfInvestment * 100;
}