Integer Division in Java: Rounding & Truncation

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: How to Round to 2 Decimal Places in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Integer Division
  • 2:31 Modulus Division
  • 3:37 Type Casting
  • 4:52 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

When dividing integers in Java, the results may not always be as expected. This lesson will use code examples to explain how Java handles division of integers as well as how numbers are rounded and truncated.

Integer Division

Integer division in Java might cause some frustration, but if you know what to expect when going in, you can take some steps to alleviate these snags. Let's start by looking at some code. The following snippet is straightforward because the integers divide evenly. We declare two integers with values, then create another one for the division. To divide in Java, we use the forward slash (/).

int a = 25;
int b = 5;
int c = a / b;

What happens if the numbers don't divide evenly? What if you try to divide 10 by 3? The correct value is 3.3333..... and so on. Since the variables are integers, Java cannot store any of the values past the decimal point.

If we input 10 as the numerator and 3 as a divisor, Java only truncates:

Java truncates division

It makes sense to truncate 3.3 to 3; but what if we divided 15 by 4? The mathematical result would be 3.75. Will Java round up or round down?

The code that you can use to accept user input and complete the calculation follows. We won't focus on the mechanics of this, but instead on the nuances of integer division.

int a, b;
System readme
System.out.println("Enter Numerator");
a = readme.nextInt();
System.out.println("Enter Divisor");
b = readme.nextInt();
System.out.println("Result = " + a / b);

When we run the previous code and input 15 for the numerator and 4 for the divisor, Java displays the following output:

Java divide integers truncated

If Java were rounding the value, the result would be 4. Instead, it simply chops off anything to the right of the decimal point, leaving only the integer value. According to Java's internal working, this truncation is actually rounding. Java refers to this as rounding toward zero.

Even though the value is truncated, or rounded, toward zero, it doesn't mean the value is lost. Java actually can tell you if there is a remainder from your operation.

Modulus Division

In our code, we performed simple division (a/b). 15 / 4 was 3.75, and since both variables were integers, Java cut off the .75 and displayed 3. However, we CAN figure out what the remainder was, if any. This is done with the modulus operator (%).

Let's try it out. After the division, we'll add a line to display the modulus operation:

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account