tags:

views:

98

answers:

4

Hello,

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

+2  A: 

Please try it-

  $tempMod = (float)($x / $y);
  $tempMod = ($tempMod - (int)$tempMod)*$y;
Sadat
-1 OP wants PHP, not C or Java or other languages.
BoltClock
but its a logic :(
Sadat
Thanks for the edit. I revoked my downvote.
BoltClock
+8  A: 
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

Mark Baker
I need only the decimal value
Jean
For the record, the last technique mentioned is the fastest, as there are fewer floating point operations being performed.
mattbasta
+2  A: 

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.

mattbasta
I need only the decimal value
Jean
A: 

Use Modulus operator

19 % 5;
Ghost
Please read the question and all its comments before writing an answer.
BoltClock