Alex Rivera | Logout

Integer math in c#

Asked 2008-10-30T13:35:03.580
9

I have a menu of product brands that I want to split over 4 columns. So if I have 39 brands, then I want the maximum item count for each column to be 10 (with a single gap in the last column. Here's how I'm calculating the item count for a column (using C#):

int ItemCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(BrandCount) / 4m));

All that conversion seems really ugly to me. Is there a better way to do math on integers in C#?

Edit
Report

1 Answer

2

Perhaps try something like this ... Assuming BrandCount is an integer. You still have the same casts, but it might be clearer:

int ItemCount = (int)(Math.Ceiling(BrandCount / 4m));

I'm not a huge fan of the Convert class, and I avoid it whenever possible. It always seems to make my code illegible.

answered 2008-10-30T13:41:35.470

Your Answer