Reputation: 61
I'm fetching data froma remove endpoint. Data response only contains total item count but no total page. How can I calculate total pages given that I only have total item count and per page item count? I need this because I'm trying to setup paging library
for example:
perPage = 10
totalItemCount = 10
totalPages = 1
The idea is totalItemCount is greater than 10 + 1 but lower than or equal 20, totalPages would be 2; And totalItemCount > 10 + 10 + 1 but lower than or equal 30, totalPages = 3 and so on...
I couldn't make a good algo to calculate this using Kotlin
Upvotes: 0
Views: 1748
Reputation: 8064
This seems to work. I have tried it in https://pl.kotl.in/hUDoamkG3:
fun main()
{
val listOfTotalItemCounts = listOf(0, 1, 9, 10, 15, 19, 20, 21, 124)
val perPage = 10
for (totalItemCount in listOfTotalItemCounts)
{
var result = (totalItemCount / perPage)
result = if (totalItemCount % perPage == 0) result else (result + 1)
print("When totalItemCount = ")
print(totalItemCount)
print(" ---> result = ")
println(result)
}
}
It yields the following output:
When totalItemCount = 0 ---> result = 0
When totalItemCount = 1 ---> result = 1
When totalItemCount = 9 ---> result = 1
When totalItemCount = 10 ---> result = 1
When totalItemCount = 15 ---> result = 2
When totalItemCount = 19 ---> result = 2
When totalItemCount = 20 ---> result = 2
When totalItemCount = 21 ---> result = 3
When totalItemCount = 124 ---> result = 13
Upvotes: 2