How to format a Java int or long with printf example
How use Java printf to format int values
It’s much easier to format an int
with printf
than a float or double, because decimal precision is not a concern.
To use Java’s printf
to format an int, long, short or byte value, follow these rules:
- Use
%d
as the placeholder specifier. - Precede the letter d with a comma to add a thousands group separator.
- Add additional parameters to left or right justify, add a plus sign, or zero pad.
Java int printf example
Here is a simple example of both an int and a long formatted with Java’s printf
function:
package com.mcnz.rps; public class JavaPrintfIntExample { /* Java printf int code example. */ public static void main(String[] args) { int eresting = 54321; long johns = 98765L; System.out.printf("%,d :: %d", eresting, johns); /* Printf int example prints: 54,321 :: 98765 */ } }
Java int printf function explained
There are two key takeaways from this Java int printf
example:
- The
%d
specifier works for long, int, double and short datatypes. - The inclusion of a comma adds a grouping separator for large numbers.
Be careful not to confuse the %d
and %f
specifiers. The %d
specifier is used for integer types, while %f
is only used with a Java float or double.
Java integer printf syntax
The full format for the Java printf
function when used with an int is as follows:
% [flags] [width] specifier-character
printf option | Function |
0 | Numbers below the specified width have zero padding. |
+ | The plus sign causes a plus or minus to precede the integer. |
, | The comma causes numbers to be grouped by the thousands. |
<space> | A blank space inserts a minus for negative integers,and a space for positive integers. |
Advanced Java in printf code example
Here is a more advanced application of the various flags, options and specifiers that you can use with printf
and integer values:
package com.mcnz.rps; public class JavaPrintfIntExample { /* Java printf int code example. */ public static void main(String[] args) { int eresting = 54321; long johns = 98765L; System.out.printf("%0,10d :: %+,7d", eresting, johns); /* Printf int example prints: 000054,321 :: +98,765 */ } }
Table of Java integer printf patterns
Here’s some sample code to generate a chart of different integer printf
patterns in Java:
System.out.printf("------------------------------%n"); System.out.printf(" Java int printf chart%n"); System.out.printf(" (number: 123457890)%n"); System.out.printf("------------------------------%n"); System.out.printf("| %-8s | %s |%n", "PATTERN", "RESULT"); System.out.printf("------------------------------%n"); System.out.printf("| %-8s | %d |%n", "%d", 123457890); System.out.printf("| %-8s | %,d |%n", "%,d", 123457890); System.out.printf("| %-8s | %,15d |%n", "%,15d", 123457890); System.out.printf("| %-8s | %+,15d |%n", "%+,15d", 123457890); System.out.printf("| %-8s | %-+,15d |%n", "%-+,15d", 123457890); System.out.printf("| %-8s | %0,15d |%n", "%0,15d", 123457890);
Sometimes it’s fun to format output with a Java printf chart.
Format output with Java printf tutorials |
---|
Here are some great tutorials and examples on how to use printf to format Java output.
|