Formatting a double in Java is the difference between readable output and a garbage column that makes your future self cry. Use System.out.printf
and format specifiers to control precision width grouping and sign handling so your console output looks intentional instead of accidental.
There are three usual suspects when formatting floating point numbers with printf
. Choose wisely and avoid the urge to try every option at once.
%f
for normal decimal notation. Good for money style or human friendly numbers.%e
for scientific notation. Use when numbers vary wildly in magnitude.%g
for compact automatic choice between %f
and %e
. Handy when you want the formatter to make the hard calls.Precision sets decimal places and width sets the total field size. A precision of two is written as %.2f
. Want a fixed column width of eight characters with two decimals use %8.2f
. Precision rounds the value and width pads it so columns line up like they were trained in boot camp.
Flags make the output human friendly. You can group thousands add a sign or left align. The most useful flags for doubles are:
,
to group thousands like 12,345.68
+
to always show the sign so positives do not feel ignored-
to left align when you want your columns less stubbornCombine flags to taste. For example %+8.2f
reserves eight characters shows two decimals and forces a sign.
Different systems have different ideas about whether a comma or a period should be the decimal separator. If you want predictable console output across environments pass a Locale
to printf
. For a US style decimal and grouping do this:
import java.util.Locale;
double value = 12345.6789;
System.out.printf(Locale.US, "%,.2f\n", value);
// prints 12,345.68
If you run the same code with Locale.GERMANY
and do not force the locale the output will use a comma as the decimal marker and a period for grouping. That can mess with parsers and test assertions unless you control locale.
Here are a few patterns you will actually use in the wild. Copy them like you mean it.
// Simple rounded and grouped value
double v = 12345.6789;
System.out.printf("Formatted %,.2f\n", v);
// Output: Formatted 12,345.68
// Fixed width with sign and grouping
System.out.printf("%+12,.2f\n", v);
// Output: +12,345.68
// Scientific notation for very large or very small numbers
double small = 0.000012345;
System.out.printf("%12.6e\n", small);
// Output: 1.234500e-05
NaN
and Infinity
and ignore numeric flags in obvious ways.-0.0
can format with a sign if you ask for it.BigDecimal
and format its value rather than relying on binary floating point rounding.%f
normal decimal%.2f
two decimal places%8.2f
width eight right aligned%-,.2f
grouped and left alignedLocale.US
to force period decimal and comma groupingThere you go. Use the right specifier pick sensible precision set a width for columns and control locale when you care about separators. Your console will look civilized and you will sleep better knowing numbers are not plotting against you.
I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!
This is a dedicated watch page for a single video.