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:

  1. Use %d as the placeholder specifier.
  2. Precede the letter d with a comma to add a thousands group separator.
  3. Add additional parameters to left or right justify, add a plus sign, or zero pad.

java printf integer format

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
Java int printf options and flags
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:

<span style="color: #003366"><span style="color: #993366">package</span> com.mcnz.rps;</span>







<span style="color: #003366"><span style="color: #993366">public class</span> JavaPrintfIntExample {</span>



  <span style="color: #339966">/* Java printf int code example. */</span>



<span style="color: #003366"><span style="color: #993366">  public static void</span> main(String[] args) {</span>



<span style="color: #003366"><span style="color: #993366">    int </span></span>eresting <span style="color: #003366">= 54321;</span>



<span style="color: #003366"><span style="color: #993366">    long </span><span style="color: #993366">johns   </span><span style="color: #003366">= 98765L</span><span style="color: #003366">;</span></span>



<span style="color: #003366">    System.<span style="color: #0000ff">out</span>.printf(<span style="color: #3366ff">"%0,10d :: %+,7d"</span>, <span style="color: #333333">eresting</span>, <span style="color: #333333">johns</span>);



</span>    <span style="color: #339966">/* Printf int example prints: 000054,321 :: +98,765 */</span>



<span style="color: #003366">  }</span>



<span style="color: #003366">}







</span>

Table of Java integer printf patterns

Here’s some sample code to generate a chart of different integer printf patterns in Java:

<span style="color: #003366">System.out.printf(<span style="color: #666699">"------------------------------%n"</span>);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">" Java int printf chart%n"</span>);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">" (number: 123457890)%n"</span>);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"------------------------------%n"</span>);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %s |%n", "PATTERN", "RESULT"</span>);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"------------------------------%n"</span>);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %d |%n",      "%d",</span>      123457890);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %,d |%n",     "%,d",</span>     123457890);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %,15d |%n",   "%,15d",  </span> 123457890); </span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %+,15d |%n",  "%+,15d",</span>  123457890);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %-+,15d |%n", "%-+,15d",</span> 123457890);</span>



<span style="color: #003366">System.out.printf(<span style="color: #666699">"| %-8s | %0,15d |%n",  "%0,15d",</span>  123457890);</span>

Sometimes it’s fun to format output with a Java printf chart.

Java integer printf example

Example code on how to format Java integer and long values with printf.

Format output with Java printf tutorials

Here are some great tutorials and examples on how to use printf to format Java output.

  • Learn how to format output with Java printf calls
  • Here’s how to format a String with the printf method
  • Sometimes you need to format a double with printf
  • Get creative! Learn to format a table with Java printf methods
  • Here’s how to handle Java user input  in 3 different ways

Cameron McKenzie Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.