F. Pajic - 5 months ago 18

Java Question

I need an explanation for this tiny java code.

`decimalNumber`

This is a part of a roman numeral converter:

`m = decimalNumber / 1000;`

decimalNumber = decimalNumber % 1000;

Answer

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.