Dear Developers all around the world, I'm trying to write a program that can perform some calculations. This is going to be a part of an in house ERP.
I have one number (Total Invoice Amount) , I need the breakdown for this number.
For example
Total Invoice Amount = X amount + 25 percent of x + 5 percent of (25 percent of x)
We know the total invoice amount, it is coming to us from external sources, so, for example, let it be 5600
x + 0.25x + 0.05(0.25x) = 5600
Mathematically we can do :
x + 0.25x + 0.0125 x = 5600
1.2625x = 5600
x = 4435.64
But I don't know how to use symbolic algebra in php. Could anyone suggest some solution ?
First things first, there are many ressources online that helps with learning math in PHP and i would recommend you look through w3schools math functions as each comes with math examples.
For your specific problem, I think you are over complicating this for yourself.
Essentially you can boil the math down to a coefficient using regular arithmetic so x + 0.25x + 0.05(0.25x)
becomes 1 + 0.25 + (0.05 * 0.25)
(about 1.2625).
Then you just divide the incoming value by the coefficient to get X.
See rough code example below:
function FindXOfN(int|float $N): int|float
{
// Base formular: Total Invoice Amount ($N) = $X amount + 25 percent of $X + 5 percent of (25 percent of $X)
// Derive coefficient based on above formular
$coefficient = 1 + 0.25 + (0.05 * 0.25);
// Calculate X
$X = $N / $coefficient;
// Return the result, rounded to 2 decimal places
return round($X, 2);
}
// Test
echo FindXOfN(5600);