简体   繁体   English

计算给定行数和每行项目限制的列数

[英]Calculate the number of columns given number of rows and limit of items per row

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.我有 11 个项目,我想放在一个有 2 行的表中,每行只能容纳 5 个项目,所以这意味着该表应该有 3 列,2 行,每行有 5 个项目,1 行有一个项目。

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.在给定某些项目计数的情况下,它不会返回正确的列数,例如,给定 16 个项目并且每行限制为 5 个项目,公式应返回 4,但它返回 5。

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 16/5 = 3.2,四舍五入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM