For a completely different approach that allows you to completely eliminate casting and floating point math ...
int result = val1 / val2;
if (val1 % val2 != 0) result++;
So in your case ...
int numDaysInMonth = System.DateTime.DaysInMonth(2009, 1);
int numWeeksInMonth = numDaysInMonth / 7;
if (numDaysInMonth % numWeeksInMonth != 0) numWeeksInMonth++;
This approach is rather verbose, but there may be some special cases where this is preferable. Technically, there should be a slight performance advantage for a modular approach, but it will be difficult for you to measure it.
In your case, I would stick to the accepted answer :-)
source
share