Division

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

Okay. I think some rethinking needs to happen.


while (strlen(substr(strrchr($quotient, "."), 1)) < 2) {

… what?
You find the last occurance of a period.
Then take a substring of length 1 from that point.
Then take the strlen of…a… substring where you’ve defined the length…
Then check if it’s less than 2, which it always will be, because you’ve specified a length of 1. (and if it fails, you get 0)

So… yeah. That… makes no sense to me. Moving on!

If I divide 10 by 10000, then your strpad is going to make my 10 into “10.0.0.” … which… isnt really very good for a number to be.


    $x = $x -($x * 2);

You mean $x *= -1; (x - 2x = -x)


           $divisor * $multiplicator;

this line doesnt do anything. What were you trying to do here?