0

I'm try to find a way to properly calculate the number of columns given the number of rows and the number of items per row.

For example:

I have 11 items I would like to lay in a table that has 2 rows and each row can only hold 5 items, so this means that the table should have 3 columns, 2 rows with 5 items each and 1 row with a single item.

I have already tried the following formula:

column_count = (item_count / row_count) + (item_count - row_count) / row_count

Which doesn't return the correct number of columns given certain item counts for example, given 16 items and a limit of 5 items per row, the formula should return 4 but it returns 5.

1 Answers1

2

You can just round up to the next int after a none interger division.

var nbColumns =  Math.Ceiling( (double)nbItem / nbRow ) ; 

16/5 = 3.2, round up to 4

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37