Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get remainder only from a division using PHP

Tags:

I am dividing 19/5 where by I have used 19/5 but I am unable to get the remainder only.

How do I get it.

Thanks Jean

like image 493
X10nD Avatar asked Jul 17 '10 19:07

X10nD


People also ask

How does PHP calculate remainder?

The fmod() function returns the remainder (modulo) of x/y.

Which operator would you use to find the remainder after division PHP?

What Does the Modulo Operator Do? If you have two variables $a and $b , calculating $a % $b —usually pronounced "a modulo b" or "a mod b"—will give you the remainder after dividing $a by $b . Modulo is an integer operator, so it converts both the operands to integers before calculating the remainder.

How do I find quotient and remainder in PHP?

PHP | intdiv() Function. intdiv stands for integer division. This function returns the integer quotient of the division of the given dividend and divisor. This function internally removes the remainder from the dividend to make it evenly divisible by the divisor and returns the quotient after division.

How can I divide two numbers in PHP?

PHP | bcdiv() Function The bcdiv() function in PHP is an inbuilt function and is used to divide two arbitrary precision numbers. This function accepts two arbitrary precision numbers as strings and returns the division of the two numbers after scaling the result to a specified precision.


2 Answers

echo 19 % 5; 

should return 4, which is the remainder of 19/5 (3 rem 4) There is no need to use floor, because the result of a modulus operation will always be an integer value.

If you want the remainder when working with floating point values, then PHP also has the fmod() function:

echo fmod(19,5.5); 

EDIT

If you want the remainder as a decimal:

either

echo 19/5 - floor(19/5); 

or

echo (19 % 5) / 5 

will both return 0.8

like image 83
Mark Baker Avatar answered Oct 02 '22 15:10

Mark Baker


Please try it-

  $tempMod = (float)($x / $y);   $tempMod = ($tempMod - (int)$tempMod)*$y; 
like image 41
Sadat Avatar answered Oct 02 '22 15:10

Sadat