Java String.format() Guide: Format Numbers, Strings & Dates
In Java, the String.format()
method is a powerful tool for formatting output in a clean and readable way. Whether you're working with integers, floating-point numbers, strings, or dates, you can use format specifiers to control how values are displayed—such as alignment, padding, precision, and data type representation. This guide walks through the most commonly used format specifiers and shows you how to format different types of values with practical code examples, making your Java programs more professional and user-friendly.
What is String.format()
in Java?
String.format()
allows you to create formatted strings using format specifiers that define how values should be displayed. It's commonly used for aligning text, formatting numbers, and displaying dates in a specific style. Here are some common data types and their format specifiers:
Integers (int, long):
%d
: Decimal integer.%o
: Octal integer.%x
or%X
: Hexadecimal integer (lowercase or uppercase).
int intValue = 42; long longValue = 1234567890L; String formattedInt = String.format("Decimal: %d", intValue); String formattedOctal = String.format("Octal: %o", intValue); String formattedHex = String.format("Hexadecimal: %X", intValue); String formattedLong = String.format("Long: %d", longValue);
Floating-Point Numbers (float, double):
%f
: Floating-point number.%e
or%E
: Scientific notation (lowercase or uppercase).%g
or%G
: General format (chooses%f
or%e
based on precision).
float floatValue = 3.1415f; double doubleValue = 123.456789; String formattedFloat = String.format("Floating-Point: %f", floatValue); String formattedScientific = String.format("Scientific: %e", doubleValue); String formattedGeneral = String.format("General: %g", doubleValue);
Strings:
%s
: String.%10s
: Right-justified string in a field of at least 10 characters (adjust the number as needed).
String stringValue = "Hello, World!"; String formattedString = String.format("String: %s", stringValue); String formattedRightJustified = String.format("Right-Justified: %10s", stringValue);
Dates (java.util.Date, java.time.LocalDate, etc.):
%t
: Date/time.
import java.util.Date; Date currentDate = new Date(); String formattedDate = String.format("Date: %tF", currentDate);
Padding using String.format
you can also pad different data types using String.format
by specifying width and alignment options within the format specifiers. Here are examples for different data types:
Pad Integers (int, long):
You can specify the minimum width and pad with leading zeros or spaces using
%d
for integers.To pad with leading zeros:
int intValue = 42; String formattedInt = String.format("Padded Integer: %05d", intValue); // At least 5 characters wide with leading zeros
To pad with spaces (right-justified):
int intValue = 42; String formattedInt = String.format("Padded Integer: %5d", intValue); // At least 5 characters wide with spaces
Pad Floating-Point Numbers (float, double):
You can specify the minimum width and pad with leading zeros or spaces using
%f
for floating-point numbers.To pad with leading zeros:
double doubleValue = 3.1415; String formattedDouble = String.format("Padded Double: %07.2f", doubleValue); // At least 7 characters wide with leading zeros and 2 decimal places
To pad with spaces (right-justified):
double doubleValue = 3.1415; String formattedDouble = String.format("Padded Double: %10.2f", doubleValue); // At least 10 characters wide with spaces and 2 decimal places
Pad Strings:
You can specify the minimum width and pad with spaces using
%s
for strings.To right-pad with spaces:
String stringValue = "Hello"; String formattedString = String.format("Padded String: %-10s", stringValue); // At least 10 characters wide with left-justified text
To left-pad with spaces:
String stringValue = "Hello"; String formattedString = String.format("Padded String: %10s", stringValue); // At least 10 characters wide with right-justified text
String.format()
is a versatile method for producing clean, readable output in Java. By using format specifiers, you can control how text, numbers, and dates are displayed in your application, with options for padding, alignment, and precision. Practice using different specifiers in your code to enhance both functionality and presentation. Explore more formatting features and Java tips at itcodescanner.com to continue refining your programming skills.
FAQ Section
Q1. What is the difference between %f
and %e
in Java formatting?
%f
displays decimal format (e.g., 3.14), while %e
uses scientific notation (e.g., 3.14e+00).
Q2. How can I pad a number with leading zeros using String.format()?
Use %0Nd
where N
is the total width. Example: %05d
pads 42
as 00042
.
Q3. Can I format strings to align left or right?
Yes. %10s
right-aligns; %-10s
left-aligns the string in a 10-character field.
Q4. How do I format a date using String.format()?
Use date/time format specifiers like %tF
for ISO format (YYYY-MM-DD
) with java.util.Date
or LocalDate
.
Q5. Is String.format() locale-sensitive?
Yes. You can pass a Locale
object as the first argument to format numbers and dates in a locale-specific way.
Leave a Comment