Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
Advertisements
