Get remainder only from a division using PHP

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


Please try it-

  $tempMod = (float)($x / $y);
  $tempMod = ($tempMod - (int)$tempMod)*$y;

Depending on what language you're using, % may not be the modulus operator. I'll assume you're using PHP, in which case it is %.

From what I can see, there is no need to use floor() with integer modulus, it will always return an integer. You can safely remove it.

To me, it looks like it isn't the math that's giving you hell, it's the code around it. You'll need to post more code; the code you have listed is fine.

Edit:

You're not looking for the remainder, you're looking for the left over decimal value. It has no name.

$leftover = 19 / 5;
$leftover = $leftover - floor($leftover);

This should be what you're looking for.

Tags:

Php