Apply Formatting to Numbers, Dates, and Text in Java
Leave feedback
Overview
GroupDocs.Assembly for Java provides comprehensive formatting capabilities for numbers, dates, and text in templates. You can use format strings (the same patterns as Java’s DecimalFormat and SimpleDateFormat) and built-in text transformation functions to control how data is displayed in your documents.
The main syntax elements are:
Format strings: <<[expression]:"format">> - apply DecimalFormat / SimpleDateFormat patterns
Text functions: upper, lower, caps, firstCap - transform text case
Here are the steps to apply formatting:
Use format strings in expression tags for numbers and dates
Apply text transformation functions for string formatting
Combine formatting with expressions and calculations
Test formatting to ensure desired output
Note
Format strings follow Java formatting conventions: numeric values are formatted with DecimalFormat patterns (#,##0.00, 0.00%, etc.) and java.util.Date values with SimpleDateFormat patterns (yyyy-MM-dd, etc.). .NET standard format specifiers such as C, N2, or P are not supported. Decimal and group separators, currency symbols, and month names are taken from the default locale of the JVM; the sample outputs on this page are for the en-US locale.
Created: <<[report.getCreatedDate()]:"yyyy-MM-dd HH:mm">>
Due Date: <<[report.getDueDate()]:"MMMM dd, yyyy">>
Common date format strings:
"yyyy-MM-dd" - 2024-01-15
"MM/dd/yyyy" - 01/15/2024
"MMMM dd, yyyy" - January 15, 2024
"dd.MM.yyyy" - 15.01.2024
"HH:mm" - 14:30
Note
Date format strings are applied to java.util.Date values. Values of java.time types such as LocalDate are output using their toString() method, and the format string is ignored, so convert them to java.util.Date before passing them to the engine.
Quantity: <<[order.getQuantity()]>>
Unit Price: <<[order.getUnitPrice()]:"$#,##0.00">>
Total: <<[order.getQuantity() * order.getUnitPrice()]:"$#,##0.00">>
Note
Use primitive numeric types (int, double, etc.) or their wrappers for values involved in template arithmetic. BigDecimal values can be output and formatted, but arithmetic operators cannot be applied to them in template expressions.
Custom Numeric Formats
Use custom format strings for specific requirements:
The template above produces “Population: 1,234,567,890”, “Code: 042”, and “Rate: 12.34%”.
Note
DecimalFormat patterns do not support digit masks with literal characters between digits (such as a phone number pattern (###) ###-####). Store such values as preformatted strings instead.
Formatting in Different Document Types
Formatting works consistently across document types:
importcom.groupdocs.assembly.*;importjava.util.Date;publicstaticvoidformatInSpreadsheet()throwsException{Invoiceinvoice=newInvoice();invoice.setAmount(1500.75);invoice.setCreatedDate(newDate());DocumentAssemblerassembler=newDocumentAssembler();assembler.assembleDocument("FormattingTemplate.xlsx","FormattingOutput.xlsx",newDataSourceInfo(invoice,"invoice"));System.out.println("Formatting applied in spreadsheet successfully.");}
Warning
Format strings and text function names are case-sensitive. Follow DecimalFormat and SimpleDateFormat conventions exactly (for example, MM means months while mm means minutes), and write text functions exactly as upper, lower, caps, and firstCap.
Advanced Usage Topics
To learn more about custom format providers, culture-specific formatting, and advanced formatting scenarios, please refer to the advanced usage section.
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples: