How use Java printf to format a double’s decimals
To make use of Java printf
to format double and float values with decimal place precision, comply with these two guidelines:
- Use
%f
because the double specifier within the textual content string. - Precede the letter f with a decimal and quantity to specify precision.
Java double printf instance
Right here is a straightforward instance of each a float and double being formatted with Java’s printf
perform:
bundle com.mcnz.rps; public class JavaPrintfDoubleExample /* Java double printf code instance. */ public static void foremost(String[] args) double mint = 1234.12345; float sum = 1234.12345f; System.out.printf("%,.3f :: %,.5f", mint, sum); /* Instance prints: 1,234.123 :: 1234.12345 */
Java double printf perform defined
There are three key takeaways from this Java printf
instance for decimal locations:
- The
%f
specifier works with each double and float values. - The decimal and quantity after the % specify the variety of decimals to print.
- A comma after the % signal provides a grouping separator for giant numbers.
Is the Java printf specifier %f or %d for floating-point doubles?
Many individuals confuse the %d
and %f
printf
specifiers.
Each %d
and %f
are legitimate when used with the Java printf
perform. Nonetheless, solely %f
is meant to be used with floating level values reminiscent of floats and doubles.
This fast chart exhibits the way to use Java printf with double sorts.
The %d
specifier is for use with non-floating level decimal whole-numbers reminiscent of byte, brief, int and lengthy.
Java double printf syntax
The format for the Java printf
perform is as follows:
% [flags] [width] [.precision] specifier-character
printf flag | Function |
+ | Will pressure the quantity to be preceded by a plus or minus signal |
0 | Leads to zero padding for numbers beneath the required width |
, | The grouping separator for giant values |
<area> | A clean area will present a minus signal for destructive numbers and an area for constructive numbers |
Superior Java decimal printf code instance
Here’s a extra superior Java printf
format code instance with double values:
public class AdvancedJavaPrintfDoubleExample /* Superior Java printf double instance */ public static void foremost(String[] args) double iLovePie = Math.PI * 999 * -1; System.out.printf("Worth is: %0,10.2f", iLovePie); /* This printf double instance outputs 'Worth is: -03,138.45' */
The output of this code instance which makes use of %0,10.3f because the format specifier is:
Worth is: -03,138.45
The %0,10.3f format specifier breaks down like this:
- The quantity generated totals 10 characters in width, together with the signal, radix and grouping comma.
- Zero padding on the left ensures all 10 characters are used.
- Precision is to 2 decimal locations.
- A comma teams massive numbers by the 1000’s.
How can Java printf format a double to 2 decimal locations?
It’s a widespread requirement to format currencies to 2 decimal locations.
You possibly can simply obtain this with the Java printf
perform. Simply use %.2f because the format specifier. This can make the Java printf
format a double to 2 decimal locations.
/* Code instance to print a double to 2 decimal locations with Java printf */
System.out.printf("I like %.2f lots!", Math.PI);
With out Java printf
, double formatting is a troublesome problem. However with this easy methodology constructed proper into the Java API, you may make floating level knowledge straightforward to learn and straightforward on your customers to grasp.
Java double printf desk instance
The chart above, which exhibits how completely different patterns affect the formatting of a double, was truly created with printf
statements.
Right here’s the total code:
System.out.printf("------------------------------%n"); System.out.printf(" Java double printf chartpercentn"); System.out.printf(" (quantity: 12345.12345)%n"); System.out.printf("------------------------------%n"); System.out.printf("| %-8s | %s |%n", "PATTERN", "RESULT"); System.out.printf("------------------------------%n"); System.out.printf("| %-8s | %f |%n", "%f", 12345.12345); System.out.printf("| %-8s | %15f |%n", "%15f", 12345.12345); System.out.printf("| %-8s | %-15f |%n", "%-15f", 12345.12345); System.out.printf("| %-8s | %-15.3f |%n", "%-15.3f", 12345.12345); System.out.printf("| %-8s | %015.3f |%n", "%015.3f", 12345.12345); System.out.printf("------------------------------%n");
Typically it’s enjoyable to make charts tables with Java’s printf
command.

Right here’s the code and output of a desk of Java double printf examples.