Skip to main content

Truncation in AP Computer Science A

In AP Computer Science A, truncation is what happens when Java casts a double to an int. The digits to the right of the decimal point are simply dropped, not rounded, so (int) 3.99 becomes 3 and (int) -3.99 becomes -3 (EK 1.5.A.2).

This definition is part of Fiveable's AP Computer Science A review, which also has guided practice questions.

Verified for the 2027 AP Computer Science A exam•Last updated October 2026•Fiveable Content Team

What is Truncation?

Truncation means cutting off part of a value and throwing it away. In AP CSA, the version you need is very specific. When you cast a double to an int with the (int) cast operator, Java keeps the whole-number part and deletes everything after the decimal point. That is exactly what EK 1.5.A.2 describes: "Casting double value to an int value causes the digits to the right of the decimal point to be truncated."

The key mental model is that truncation is a chop, not a round. (int) 9.999 is 9, not 10. For negative numbers the chop still moves toward zero, so (int) -7.8 is -7, not -8. Think of it like a paper cutter that slices off the decimal tail no matter how big it is. You'll see the same chopping behavior when two int values are divided, because 7 / 2 gives 3 and the .5 disappears.

Why Truncation matters in AP® Computer Science A

Truncation lives in Unit 1: Using Objects and Methods, inside Topic 1.5 Casting and Ranges of Variables. It directly supports learning objective AP Comp Sci A 1.5.A, which asks you to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced. That last part is the trap. Many MCQs come down to whether you remember that Java truncates instead of rounding.

It also sets up the rest of 1.5. EK 1.5.A.4 gives the rounding trick (int)(x + 0.5), which only works because the cast truncates. And 1.5.C (accuracy limits and round-off error) completes the picture of all the ways a number can quietly lose information in Java. Once you get truncation, code-tracing questions in later units get easier too, since loops and array index math often divide integers.

How Truncation connects across the course

Cast Operators and Widening (Unit 1)

Casting goes two directions. Going from int to double is widening (EK 1.5.A.3). It happens automatically and loses nothing, so 5 becomes 5.0. Going from double to int is narrowing, and narrowing is where truncation happens. If you can name which direction the cast is going, you know whether data might get chopped.

Integer Division in Arithmetic Expressions (Unit 1)

When both operands are int, Java does integer division and truncates the result. So (score1 * weight1 + score2 * weight2) / (weight1 + weight2) gives a chopped whole number even if the method returns a double. The truncation happens before the return, and putting the result in a double afterward can't bring the lost decimal back.

Rounding with (int)(x + 0.5) (Unit 1)

EK 1.5.A.4 turns truncation into a rounding tool. Adding 0.5 first means anything at .5 or above gets pushed past the next whole number before the chop. For negatives the move flips to (int)(x - 0.5), since truncation chops toward zero and the nudge has to go the other way.

Round-off Error and Integer Overflow (Unit 1)

Truncation is one of three ways Topic 1.5 shows a number losing accuracy. Truncation drops decimals on purpose during a cast. Round-off error (EK 1.5.C.1) happens when a double is too precise to store. Overflow (EK 1.5.B.3) happens when an int goes past Integer.MAX_VALUE. None of them throws an error, so all three are easy to miss when you trace code.

Is Truncation on the AP® Computer Science A exam?

Truncation shows up mostly in multiple-choice code-tracing questions. You'll see a short segment with a cast or a division, and you have to give the exact value printed. Typical setups include:

  • Predict the output of a cast. For example, (int) 4.7 prints 4, not 5.
  • Spot a broken rounding attempt. If double value = -7.8; is followed by (int)(value + 0.5), the result is (int)(-7.3), which truncates to -7. The correct nearest integer is -8, and you'd get it with (int)(value - 0.5).
  • Fix a method that truncates by accident. A weighted-average method that divides int by int loses its decimal. The fix is to cast to double before the division, for example (double)(score1 * weight1 + ...) or 1.0 * .... Some versions also ask which fix reduces overflow risk. Doing the multiplication in double handles both problems at once.
  • Average large ints safely. (score1 + score2 + score3) / 3 can overflow during the sum and truncate during the division, so you have to deal with both issues.

No released FRQ has used the word "truncation" verbatim. But FRQs that compute averages, percentages, or array midpoints still depend on it, so knowing when division truncates keeps your free-response code correct.

Truncation vs Rounding

Rounding goes to the nearest integer, so 3.7 rounds to 4. Truncation just deletes the decimal, so 3.7 becomes 3. Java's (int) cast always truncates and never rounds. To round, you have to build it yourself with (int)(x + 0.5) for non-negative values or (int)(x - 0.5) for negative values (EK 1.5.A.4). Truncation also moves negatives toward zero, so (int) -3.7 is -3, while rounding would give -4.

Key things to remember about Truncation

  • Casting a double to an int with (int) truncates, which means Java drops every digit after the decimal point without rounding.

  • Truncation always moves toward zero, so (int) 5.9 is 5 and (int) -5.9 is -5.

  • Dividing an int by an int also truncates, so 7 / 2 evaluates to 3 even if you store the result in a double.

  • To round a double to the nearest integer, use (int)(x + 0.5) for non-negative numbers and (int)(x - 0.5) for negative numbers.

  • To keep the decimal in a division, make at least one operand a double before the division happens, for example by casting with (double) or multiplying by 1.0.

  • Truncation, round-off error, and integer overflow all lose information silently, so trace every cast and division carefully on code-tracing questions.

Frequently asked questions about Truncation

What is truncation in AP Computer Science A?

Truncation is what happens when you cast a double to an int in Java. The digits to the right of the decimal point are cut off, so (int) 8.6 becomes 8. It's covered in Topic 1.5 under EK 1.5.A.2.

Does (int) round a double in Java?

No. The (int) cast always truncates, so (int) 2.99 is 2, not 3. If you want rounding, add 0.5 before casting for non-negative values, as in (int)(2.99 + 0.5), which gives 3.

What is the difference between truncation and rounding in Java?

Rounding picks the nearest integer, while truncation just deletes the decimal part. For -7.8, truncation gives -7 and correct rounding gives -8. That's why (int)(value + 0.5) fails for negatives and you need (int)(value - 0.5) instead.

Does storing an int division result in a double fix truncation?

No. In double avg = 7 / 2; the division runs on two ints first and produces 3, which is then widened to 3.0. You have to make an operand a double before dividing, as in 7 / 2.0 or (double) 7 / 2, to get 3.5.

Is truncation the same as integer overflow?

No. Truncation drops the decimal part when a double becomes an int. Overflow (EK 1.5.B.3) happens when an int calculation goes past Integer.MAX_VALUE or below Integer.MIN_VALUE and wraps to a wrong value. Both can show up in the same average-calculation question, so check for each one.