18

I remember in java that, the modulo operator could be inverted so that rather than seeing what the remainder is of an operation, you could invert it, so instead it will tell you many times a number was divided by:

Console.WriteLine(1000 % 90);
Console.WriteLine(100 % 90);
Console.WriteLine(81 % 80);
Console.WriteLine(1 % 1);

Output:

  • 10
  • 10
  • 1
  • 0

Examples courtesy of DotNetPerls

Rather than seeing the remainder, I want to see how many times '80' went into '81'. Which should be 1 with a remainder of 1.

Does the c# modulo operator support this behaviour? If not, how might achieve the desired behaviour? With minimal code please ... :D

EDIT:

I imagine the answer is going to be something simple like dividing the two numbers and getting rid of the '-.#' value and keeping the integer '1.-'. I know about this but there must be a slicker way of doing this?

4
  • 7
    Console.WriteLine(1000 / 90) :) Jul 14, 2012 at 22:09
  • 8
    If you tagged your question integer-division then how can you not know this? Jul 14, 2012 at 22:10
  • 2
    Just divide integer not floating point numbers. 1000.0 / 90.0 = 11.111; 1000 / 90 = 11. 1000 % 90 = 10. Jul 14, 2012 at 22:13
  • 1
    @GregS, easy mistake to make! I am actually working with decimals. I could not find a decimal division tag. So I chose the next closest thing, =D Jul 14, 2012 at 22:21

6 Answers 6

24

What you are looking for is called integer division. It is not related to the modulo operator at all.

To perform an integer division, simply ensure that neither operand is a float/double.

Example:

int one = 81 / 80;

This gives you 1 while double notOne = 81.0 / 80 would give you 1.0125 for example.

1
  • TY - I have been doing C# for a few years now so something as trivial as this never really came up until now. IN java I was taught about this, all using the modulo operator. In C# I don't remember seeing anything like this. Hence the question. Jul 14, 2012 at 22:17
10

You already got the answer, no need to deal with the decimals if you assign it to an integer.

In your comment you say that you are working with decimals, then Math.Floor is a possibility. ie:

double d = Math.Floor(81.0 / 80.0); // 1.0000....
4
  • thanks for this ... (95.45 / 50) <-- just need to know how many times 50 does into 95.45. Let me check out Math.Floor out first - = ) Jul 14, 2012 at 23:04
  • 1
    95.45/50=1.909, Math.Floor will round this down to 1.000. 195.45/ 50=3.909, Math.Floor will return 3.000. etc. Jul 14, 2012 at 23:07
  • 1
    The opposite of Math.Floor is Math.Ceiling which will instead round up to the nearest number. Jul 14, 2012 at 23:09
  • WARNING note for .NET: System.Math.Floor(2,3) returns 0 (correct) while System.Math.Floor(-2,3) returns -1 (incorrect). This solution in working for positive results, only, and therefore NOT recommended.
    – Jochen
    Apr 28 at 13:10
4

In OpenOffice CALC there is QUOTIENT, which returns the integer part of a division ᎓

      MOD(17;4) returns 1, while
 QUOTIENT(17;4) returns 4.

      MOD(33;9) returns 6, while
 QUOTIENT(33;9) returns 3.

I've always thought of QUOTIENT as MOD's counterpart  —  perhaps this is what the OP meant by "opposite" of MOD.

1
  • This is exactly what I was trying to remember. The use of say, int x = 22/12 returns 1, because of the truncation in c makes the int the largest full number and discards the floating-point. If you need the remainder and the total groups you could use this in conjunction with mod Jun 30, 2020 at 20:11
2
Console.WriteLine(1000 % 90); // modulo = 10
Console.WriteLine(1000 / 90); // integer division = 11
Console.WriteLine(1000 / 90.0); // floating point division = 11.1111111111111

So I kinda get your question even though everyone else is on your case about it. In order to balance integer division you need to have the modulo operator in order to handle the remainder: ((1000 / 90) * 90) + (1000 % 90) == 1000.

1
  • Thanks it is a weird one I have to admit, it's just the ex java guy in me that bubbled up the surface and refused to be contained any longer - lol Jul 14, 2012 at 22:23
2

Luckily you're asking a different question from what an inverse of a modulo would be. An inverse of a modulo would theoretically be able to give you an unknown numerator or divisor.

For instance, say you have n % d = r. You know r and d, and n is unknown. The modulo function is expressed as n % d = n - d*INT(n/d). So r = n - d*INT(n/d). I can't think of how this can be directly solved, since you would need to know INT(n/d) when you don't know n.

0

For .NET, you might want to use System.Math.DivRem: Calculates the quotient of two numbers and also returns the remainder in an output parameter.

quotient = System.Math.DivRem (x, y, bufferForRemainder);

also see https://learn.microsoft.com/en-US/dotnet/api/system.math.divrem?view=net-7.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.