---
name: expressions-groups
description: Design JasperReports typed calculation expressions, variables, groups, subtotals, running and report totals, and their calculation, reset, increment, and evaluation times. Use when totals are wrong, missing, repeated, shown too early, split across group breaks, or depend on sorting and null values; not for appearance-only changes or data-source connection problems.
---
# Calculate totals with expressions and groups
## Establish the calculation contract
Identify the exact engine/Java versions, the expression language, each field's Java type and nullability, the expected grouping keys and order, and the business rules for rounding, null amounts and empty input. Ask for missing rules instead of inventing rounding modes, tax rules or what a subtotal of no values should show.
## Workflow
1. Keep types exact. Use `java.math.BigDecimal` fields and variables for monetary values, make each expression's result match its declared class, and set scale or rounding only with an explicit `RoundingMode` from the specification. Avoid `Double` arithmetic for money and integer division for ratios.
2. Choose each variable's `calculation` (`Sum`, `Count`, `DistinctCount`, `Average`, `Lowest`, `Highest`, `First`, `Nothing` or `System`), `resetType` (`Report`, `Page`, `Column`, `Group` with `resetGroup`, or `None`) and, when needed, `incrementType`. `Sum` and `Count` skip null expression values, and a sum of no values is null, so guard printed results with an explicit fallback.
3. Set evaluation times deliberately. An element evaluated `Now` shows the value at the moment its band prints: a group header showing its group's total needs `evaluationTime="Group"` with `evaluationGroup`, a title or page header showing a report total needs `Report`, and page totals need `Page`. Use `Auto` only when one element mixes values with different reset times.
4. Groups break whenever the group expression changes between consecutive records; they do not collect matching records. Order the data by the full nested group key, preferably in the query (`ORDER BY`) so the database does the work. Use report `sortField` elements only when in-memory sorting of the whole dataset is acceptable, and check nested groups use the same key order.
5. Define empty-input behavior. With `whenNoDataType="AllSectionsNoDetail"`, the tested 7.1.1 runtime still prints group headers and footers with null field and variable values. Prefer `whenNoDataType="NoDataSection"` with a `noData` band when a group-oriented report has no records, or design every group band to handle nulls. Do not hide group headers with a `$V{REPORT_COUNT} > 0` print-when guard: the count is still 0 when the first group header prints, so the tested runtime drops the first header for real data too.
6. Keep calculations in the report only when they belong to presentation. Leave business totals computed elsewhere to the application and display them as parameters rather than recomputing them differently.
## Validate and deliver
Fill with synthetic ordered input that includes null values, at least two groups, one unsorted case and empty input. Assert exact subtotal and report-total values with `BigDecimal.compareTo` or exact formatted text, and check that group headers show delayed values while footers reset per group. State commands and results, and label any calculation you could not execute as unverified.
## Sources
- [Sample reference](https://jasperreports.sourceforge.net/sample.reference/README.html)
- [Schema reference](https://jasperreports.sourceforge.net/schema.reference.html)
- [API](https://jasperreports.sourceforge.net/api/index.html)
— ‹guest · 2026-11-29› —
این نسخه با نشانهگذاری اختصاصی حساب شما نمایش داده میشود. برای نصب سریع، به تب نصب و راهاندازی بروید.