Format Flags
In this chapter you will learn:
- What are format flags
- How to justify Output
- How to append a + sign before positive numeric values
- How to use space flag to line up output values
- How to show negative numeric inside parentheses
- How to pad with zeros rather than spaces
- How to add grouping specifiers for large value numbers
- How to give output an alternate conversion format
What are format flags
Flags that controls various aspects of a conversion. All format flags are single characters and follow the % in a format specification.
The flags are shown here:
Flag | Effect |
---|---|
- | Left justification |
# | Alternate conversion format |
0 | Output is padded with zeros rather than spaces |
space | Positive numeric output is preceded by a space |
+ | Positive numeric output is preceded by a + sign |
, | Numeric values include grouping separators |
( | Negative numeric values are enclosed within parentheses |
Justifying Output
By default, all output is right-justified.
You can force output to be left-justified by placing a minus sign directly after the %
.
For instance, %-10.2f
left-justifies a floating-point number with two decimal
places in a 10-character field.
import java.util.Formatter;
// j av a 2s.c om
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
// Right justify by default
fmt.format("|%10.2f|", 123.123);
System.out.println(fmt);
// Now, left justify.
fmt = new Formatter();
fmt.format("|%-10.2f|", 123.123);
System.out.println(fmt);
}
}
The output:
The following code shows how to use justify flags to align positive and negative values.
import java.util.Formatter;
//j a v a2 s. co m
public class MainClass {
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:
Appends a + sign before positive numeric values
Adding the + flag appends a + sign before positive numeric values.
For example,
import java.util.Formatter;
//from j ava2 s. c om
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
// Right justify by default
fmt.format("%+d", 100);
System.out.println(fmt);
}
}
The output:
Line up output values
Adding the space flag outputs a space before positive values and line up the positive and negative numbers.
import java.util.Formatter;
//from j ava 2s . co 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:
( flag shows negative numeric inside parentheses
The ( flag shows negative numeric inside parentheses, rather than with a leading -.
import java.util.Formatter;
/*from j av a 2 s. c om*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%(d", -100);
System.out.println(fmt);
}
}
The output:
Pad with zeros rather than spaces
The 0 flag causes output to be padded with zeros rather than spaces.
import java.util.Formatter;
//from ja v a 2 s .co m
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("0%g\n", 123.1);
}
}
The output:
Add grouping specifiers
The comma (,) flag adds grouping specifiers.
import java.util.Formatter;
/*ja v a 2 s.co m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%,.2f", 1234567890.34);
System.out.println(fmt);
}
}
The output:
Alternate conversion format
The #
can be applied to %o
, %x
,
%e
, and %f
.
For %e
and %f
, the #
ensures that there will be a
decimal point even if there are no decimal digits.
import java.util.Formatter;
/*from j ava 2 s . c om*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%#f", 1.0);
System.out.println(fmt);
}
}
The output:
If you precede the %x
format specifier with a #
,
the hexadecimal number will be printed with a 0x
prefix.
import java.util.Formatter;
//from ja v a 2s . c o m
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%#X", 1);
System.out.println(fmt);
}
}
The output:
Preceding the %o
specifier with #
causes the number to be
printed with a leading zero.
import java.util.Formatter;
/*from j a v a 2 s. com*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%#o", 1);
System.out.println(fmt);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- How and when to use Uppercase Option for Java Formatter
- How to uppercase hexadecimal
- How to uppercase e symbol