Java: Format numbers and text with String.format

String.format builds strings using printf-style conversion specifiers.

Code
String s = String.format("%-6s|%5.2f|%,d", "Ava", 3.14159, 1000000);
System.out.println(s);
Output
Ava   | 3.14|1,000,000
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-07-26