- 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
Space format specifiers in Java
For format specifier, import the following package −
import java.util.Formatter;
Create a formatter object and set space format specifier −
Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f);
The following is an example displaying different forms of space format specifiers −
Example
import java.util.Formatter; public class Demo { public static void main(String args[]) { Formatter f = new Formatter(); f.format("% d", -50); System.out.println(f); f = new Formatter(); f.format("% d", 90); System.out.println(f); f = new Formatter(); f.format("%10d", -50); System.out.println(f); f = new Formatter(); f.format("% 10d", 90); System.out.println(f); f = new Formatter(); f.format("%12.2f", -7.598); System.out.println(f); f = new Formatter(); f.format("% 12.3f", 90.87787); System.out.println(f); } }
Output
-50 90 -50 90 -7.60 90.878
- Related Articles
- Format specifiers in C
- What are different format specifiers used in C language?
- C program to print characters without using format specifiers
- Add grouping specifiers for large numbers in Java
- Format Month in MM format in Java
- Format Minutes in mm format in Java
- Format Month in M format in Java
- Format Seconds in s format in Java
- Format Year in yy format in Java
- Format Year in yyyy format in Java
- What are the differences between protected and default access specifiers in Java?
- Java Program to format hour in H (0-23) format in Java
- Java Program to format Month in MMMM format
- Format hour in HH (00-23) format in Java
- Format hour in k (1-24) format in Java

Advertisements