How to format a Java double with printf example

How use Java printf to format a double’s decimals

To use Java printf  to format double and float values with decimal place precision, follow these two rules:

  1. Use %f as the double specifier in the text string
  2. Precede the letter f with a decimal and number to specify precision

Java double printf example

Here is a simple example of both a float and double being formatted with Java’s printf function:

package com.mcnz.rps;

public class JavaPrintfDoubleExample {
  /* Java double printf code example. */
  public static void main(String[] args) {
    double mint = 1234.12345;
    float  sum  = 1234.12345f;
    System.out.printf("%,.3f :: %,.5f", mint, sum);
    /* Example prints: 1,234.123 :: 1234.12345 */

Java double printf function explained

There are two key takeaways from this Java printf example for decimal places:

  • The %f specifier works with both double and float values
  • The decimal and number after the % specify the number of decimals to print
  • A comma after the % sign adds a grouping separator for large numbers

Is the Java printf specifier %f or %d for floating-point doubles?

Many people confuse the %d and %f printf specifiers.

Both %d and %f are valid when used with the Java printf function. However, only %f is intended for use with floating point values like floats and doubles.

The %d specifier is to be used with non-floating point decimal whole-numbers such as the byte, short, int and long.

Java double printf syntax

The format for the Java printf function is as follows:

% [flags] [width] [.precision] specifier-character
Optional Java double printf flags
printf flag Purpose
 + Will force the number to be preceded by a plus or minus sign
 0 Results in zero padding for numbers below the specified width
 , The grouping separator for large values
<space> A blank space with show a minus sign for negative numbers and a space for positive numbers

Advanced Java decimal printf code example

Here is a more advanced Java printf format code example with double values:

public class AdvancedJavaPrintfDoubleExample {
  /* Advanced Java printf double example */
  public static void main(String[] args) {
    double iLovePie = Math.PI * 999 * -1;
    System.out.printf("Value is: %0,10.2f", iLovePie);
    /* This printf double example outputs 'Value is: -03,138.45' */

The output of this code example which uses the %0,10.3f as the format specifier is:

Value is: -03,138.45

The %0,10.3f  format specifier breaks down like this:

  • The number generated will be a total of 10 characters in width, including the sign, radix and grouping comma
  • Zero padding will happen on the left to make sure all 10 characters are used
  • The precision will be to two decimal places
  • A comma will be used to group large numbers by the thousands

How can Java printf format a double to two decimal places?

It is a common requirement to format currencies to two decimal places.

This is easily achieved with the Java printf function. Just use %.2f as the format specifier. This will make the Java printf format a double to two decimal places.

/* Code example to print a double to two decimal places with Java printf */
System.out.printf("I love %.2f a lot!", Math.PI);

Without Java printf, double formatting is a difficult challenge. But with this simple method built right into the Java API, it’s not hard to make floating point data easy to read and easy for your users to understand.