Java Format Line up Space
In this chapter you will learn:
- What is space flag
- Syntax for space flag
- Example - justify flags to align positive and negative values
Description
We can use space to add a space before positive numeric output.
Syntax
fmt.format("% d", -100);
Example
The following code shows how to use justify flags to align positive and negative values.
import java.util.Formatter;
/* w w w. j ava2 s.c o m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("% d", -100);
System.out.println(fmt);
fmt = new Formatter();
fmt.format("% d", 100);
System.out.println(fmt);
fmt = new Formatter();
fmt.format("% d", -200);
System.out.println(fmt);
fmt = new Formatter();
fmt.format("% d", 200);
System.out.println(fmt);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- When to use Format Parentheses
- Syntax for Java Format Parentheses
- How to show negative numeric inside parentheses
Java Formatter Class
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Date
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Negative and Positive
Java Format Line up Space
Java Format ParenthesesJava Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Date