Steven
Steven

Reputation:

Dividing with a remainder in PHP

I have a part in my code where I need to divide and have a remainder instead of a decimal answer.

How can I do this?

Upvotes: 13

Views: 20497

Answers (9)

Romain Norberg
Romain Norberg

Reputation: 1067

It seems to be an old post, but for those who might be interested here is a very light package that could meet your needs: https://github.com/romainnorberg/residue (feedbacks are welcome)

Upvotes: 0

axiac
axiac

Reputation: 72177

The mathematical correct answer is:

remainder = dividend % divisor;
quotient = (dividend - remainder) / divisor;

and the remainder verifies the condition 0 <= remainder < abs(divisor).

Unfortunately, many programming languages (including PHP) don't handle the negative numbers correctly from the mathematical point of view. They use different rules to compute the value and the sign of the remainder. The code above does not produce the correct results in PHP.

If you need to work with negative numbers and get the mathematical correct results using PHP then you can use the following formulae:

$remainder = (($dividend % $divider) + abs($divider)) % abs($divider);
$quotient = ($dividend - $remainder) / $divider;

They rely on the way PHP computes modulus with negative operands and they may not provide the correct result if they are ported to a different language.

Here is a script that implements these formulae and checks the results against the values provided as example in the aforementioned mathematical correct answer.

Upvotes: 4

K.Alex
K.Alex

Reputation: 91

An example to show strings like 1 hour 6 minutes using floor() and modulus (%) if only minutes/seconds given:

$minutes=126;

if($minutes < 60) {
    $span= $minutes.' min.';
} else {
    $rem=$minutes % 60;
    $span=floor($minutes/60).' h. '. (($rem>0) ? $rem.' min.':'');
}

// echo 'Hello Jon Doe, we notify you that even will last for 2 h. 6 min.
echo 'Hello Jon Doe, we notify you that event will last for '.$span;

Upvotes: 0

Vineesh K S
Vineesh K S

Reputation: 1964

Use This Function Its an array Description array gmp_div_qr ( resource $n , resource $d [, int $round ] )

The function divides n by d .

reference : http://php.net/manual/en/function.gmp-div-qr.php

Upvotes: 0

luckyape
luckyape

Reputation: 722

I had to develop this approach because my numerator was a float value and modulus was rounding results.

Using Raffaello's approach offered here for dividing floats and taking from Sam152's solution above came up with the following.

$a = 2.1;
$b = 8;
$fraction = $a / (float) $b;
$parts = explode('.', $fraction);                
$int = $parts[0];
$remainder = $score - ($int*$b) ;  

Upvotes: 0

Sam Becker
Sam Becker

Reputation: 19636

You can do what you are describing using the "%" (modulus) operator. The following code is an example of dividing with a remainder.

$remainder=$num % $divideby;
$number=explode('.',($num / $divideby));
$answer=$number[0];
echo $answer.' remainder '.$remainder;

Upvotes: 17

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

If you need to look it up, the % operator is called mod (or modulus).

Upvotes: 1

Gumbo
Gumbo

Reputation: 655139

A solution for positive and negative numbers:

$quotient = $dividend / $divison;
$integer = (int) ($quotient < 0 ? ceil($quotient) : floor($quotient));
$remainder = $dividend % $divisor;

Upvotes: 5

Alex Barrett
Alex Barrett

Reputation: 16455

$quotient = intval($dividend / $divisor);
$remainder = $dividend % $divisor;

Using intval instead of floor will round the quotient towards zero, providing accurate results when the dividend is negative.

Upvotes: 24

Related Questions