Java Format Negative and Positive
In this chapter you will learn:
Description
We can use + sign to add a + sign to positive numeric output.
Syntax
fmt.format("%+d", 100);
Example
Adding the + flag appends a + sign before positive numeric values.
For example,
import java.util.Formatter;
//from w w w . j a va 2 s . c o m
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:
Next chapter...
What you will learn in the next chapter:
- What is space flag
- Syntax for space flag
- Example - justify flags to align positive and negative values
Java Formatter Class
Java Format Specifier
Java Format Specifier Uppercase
Java Format Precision
Java Format Flags
Java Format Justifying Output
Java Format Parentheses
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 SpaceJava Format Parentheses
Java Format Zero Padding
Java Format Comma
Java Format Alternate Conversion
Java Format Argument Index
Java Format date time
Java Format Date