Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer math in c#

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#?

like image 297
Ben Mills Avatar asked Oct 30 '08 13:10

Ben Mills


1 Answers

You can cast:

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

Also, because int/decimal results in a decimal you can remove one of the casts:

int ItemCount = (int) Math.Ceiling( BrandCount / 4m );
like image 89
kͩeͣmͮpͥ ͩ Avatar answered Dec 10 '22 01:12

kͩeͣmͮpͥ ͩ