-2

I'm trying to round numbers down to the lowest 100. Basically 199 -> 100, 2021 -> 2000, etc. How can I make this in C# program?

N.Campos
  • 25
  • 3

1 Answers1

5

This will do it:

private static int RoundDown(int input)
{
    return input / 100 * 100;
}

The reason this works is as follows:

Dividing an integer value by 100 will give the integer portion of the result. This means, if we take the integer 199 and divide by 100, the mathematical result is 1.99 but because we're working with integers, only the 1 is retained. Multiplying this value again by 100 will give you 100 which is the desired result.

Kesty
  • 610
  • 9
  • 19