F. Pajic F. Pajic - 1 year ago 62
Java Question

Can someone explain me this code

I need an explanation for this tiny java code.

is a variable for user input and M for roman numeral 1000
This is a part of a roman numeral converter:

m = decimalNumber / 1000;
decimalNumber = decimalNumber % 1000;

Answer Source

The first line m = decimalNumber / 1000; sets m equal to the number of thousands in decimalNumber.

Then, the second line, sets decimalNumber equal to decimalNumber mod 1000. (If you're unfamiliar with modulo, it's the remainder after division.)

So, for example, let's take the number 2453 to start with in decimalNumber. First, we set m equal to 2453 / 1000 which, in integer division, is 2. (Remember, the remainder is lost when dividing integers, but NOT when dividing floats.)

Then, decimalNumber is set equal to 2453 % 1000, which is 453.

By doing this, you're basically counting how many 'M's you need in your final number, then reducing decimalNumber by that many thousands.