- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Formatted Output in Java
String provides format() method can be used to print formatted output in java.
System.out.printf() method can be used to print formatted output in java.
Following example returns a formatted string value by using a specific locale, format and arguments in format() method
Example
import java.util.*; public class StringFormat { public static void main(String[] args) { double e = Math.E; System.out.format("%f%n", e); System.out.format(Locale.GERMANY, "%-10.4f%n%n", e); } }
Output
The above code sample will produce the following result.
2.718282 2,7183
The following is an another sample example of format strings.
Example
public class HelloWorld { public static void main(String []args) { String name = "Hello World"; String s1 = String.format("name %s", name); String s2 = String.format("value %f",32.33434); String s3 = String.format("value %32.12f",32.33434); System.out.print(s1); System.out.print("
"); System.out.print(s2); System.out.print("
"); System.out.print(s3); System.out.print("
"); } }
Output
The above code sample will produce the following result.
name Hello World value 32.334340 value 32.334340000000
- Related Articles
- Formatted output in C#
- Left justify output in Java
- Formatted string literals in C#
- Getting formatted time in Python
- How to print a formatted text using printf() method in Java?\n
- Format Output with Formatter in Java
- Formatted string literals (f-strings) in Python?
- Formatted text in Linux Terminal using Python
- Formatted Strings Using Template Strings in JavaScript
- Redirecting System.out.println() output to a file in Java
- Formatting a Negative Number Output with Parentheses in Java
- Set width and precision to format output in Java
- How to get formatted date and time in Python?
- How to get formatted JSON in .NET using C#?
- How to use the SimpleDateFormat class to convert a Java Date to a formatted String?

Advertisements