How to find the total number of pages when paging in Java

I use it occasionally, so make a note.

The total number of pages / number of pages per page can be obtained by rounding up to the nearest whole number.

There are various ways to do it, but if you implement the above as it is, it looks like this.

        int totalSize = 30;
        double pageSize = 10.0;
        int totalPage = (int) Math.ceil(totalSize / pageSize);
        System.out.println(totalPage);
        >> 3  
        int totalSize = 35;
        double pageSize = 10.0;
        int totalPage = (int) Math.ceil(totalSize / pageSize);
        System.out.println(totalPage);
        >> 4  
        int totalSize = 40;
        double pageSize = 10.0;
        int totalPage = (int) Math.ceil(totalSize / pageSize);
        System.out.println(totalPage);
        >> 4  
        int totalSize = 41;
        double pageSize = 10.0;
        int totalPage = (int) Math.ceil(totalSize / pageSize);
        System.out.println(totalPage);
        >> 5  

Math # ceil is rounded up, but the number of cases per page is defined as double so that a decimal point will appear when dividing.

The return value is double, but I don't need the decimal point, so I cast it to int. If it is a primitive type, non-null is guaranteed, but if something like "extract only the integer part of Double" is not an object, it will be a little unnatural.

Recommended Posts

How to find the total number of pages when paging in Java
How to derive the last day of the month in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Summary of how to use the proxy set in IE when connecting with Java
How to determine the number of parallels
How to get the date in java
How to solve the unknown error when using slf4j in Java
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
How to change the maximum and maximum number of POST data in Spark
How to find out the Java version of a compiled class file
How to constrain the action of the transition destination when not logged in
How to get the absolute path of a directory running in Java
How to find the total score and average score
[Java] How to get the authority of the folder
[Swift] How to get the number of elements in an array (super basic)
[Java] How to get the URL of the transition source
[Java] How to omit the private constructor in Lombok
How to write Scala from the perspective of Java
[Ruby] How to find the sum of each digit
[Java] How to get the maximum value of HashMap
Summary of how to implement default arguments in Java
How to find the total value, average value, etc. of a two-dimensional array (multidimensional array)-java
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
How to get the class name / method name running in Java
How to set chrony when the time shifts in CentOS7
Difference between Java and JavaScript (how to find the average)
Command to check the number and status of Java threads
Count the number of digits after the decimal point in Java
Things to be aware of when writing code in Java
How to dynamically switch JDK when building Java in Gradle
How to switch Java in the OpenJDK era on Mac
Find the number of days in a month with Kotlin
How to find May'n in XPath
How to find the average angle
How to learn JAVA in 7 days
Considering the adoption of Java language in the Reiwa era ~ How to choose a safe SDK
How to manage the difference in each environment with yml without increasing the number of RAILS_ENV
How to use classes in Java?
How to name variables in Java
How to find Java prime numbers
How to concatenate strings in java
Find out the list of fonts available in AWS Lambda + Java
How to output the value when there is an array in the array
[jOOQ] How to CASE WHEN in the WHERE / AND / OR clause
How to get the id of PRIMAY KEY auto_incremented in MyBatis
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
How to check for the contents of a java fixed-length string
How to write the view when Vue is introduced in Rails?
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Method definition location Summary of how to check When defined in the project and Rails / Gem
Get the result of POST in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
[java] Summary of how to handle char
[Java] How to use the HashMap class
How to do base conversion in Java
[Java] How to use the toString () method