I am trying to make a code that performs a division however I am not having success and don´t really know how to correct the problem, for example division of 1000 by 10 gives me 10.00 which is wrong, can someone help me by taking a look at my code:
<?php
$dividend = 1000;
$divisor = 10;
$quotient = null;
$reminder = $dividend;
$multiplicator = 2;
$decimal = false;
while (strlen(substr(strrchr($quotient, "."), 1)) < 2) {
if (strlen($dividend) <= strlen($divisor)) {
if ($decimal === false) {
$dividend = str_pad($dividend, strlen($divisor) + 2, '.0');
$decimal = true;
} else {
$dividend = str_pad($dividend, strlen($divisor) + 1, 0);
}
if (strlen(strstr($dividend, '.')) > 1) {
if (strlen(strstr($quotient, '.')) <= 0) {
$quotient = $quotient . '.';
}
}
}
$x = strlen($dividend) - strlen($divisor);
$x = $x -($x * 2);
$dividend_ = substr($dividend, 0, $x);
if ($dividend_ < $divisor) {
$quotient = $quotient . '0';
} else {
while ($divisor < $dividend_) {
$divisor * $multiplicator;
$multiplicator ++;
}
$multiplicator --;
$quotient = '' . $quotient . $multiplicator;
}
//
echo '<br/>' . $dividend;
echo '<br/>' . $quotient;
$dividend = substr($dividend, 1);
$multiplicator = 2;
}
echo '<br/>' . $dividend_;
echo '<br/>' . $quotient;
?>
I know I can do 1000/10 but I am studying and was asked to do it this way is just that I can´t figure out how to correctly place the decimal point, I either end up with no decimal point, the decimal point being 10.00 or 1000.00 but cant get it to be 100.00 I get those by changing line 16 strlen(strstr($dividend, ‘.’)) > 1 I change the last number from 1 through 3 1 gives me 10.00 2 gives me 1000.00 and 3 goes to an eternal loop because no decimal point is placed