Java Format Alternate Conversion
Description
# sets 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.
Syntax
fmt.format("%#f", 1.0);
Example
import java.util.Formatter;
/* w w w . ja va 2 s. c o m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%#f", 1.0);
System.out.println(fmt);
}
}
The output:
Example 2
If you precede the %x
format specifier with a #
,
the hexadecimal number will be printed with a 0x
prefix.
import java.util.Formatter;
//w ww. j a va 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:
Example 3
Preceding the %o
specifier with #
causes the number to be
printed with a leading zero.
import java.util.Formatter;
/* w w w.j a v a2 s .co m*/
public class Main {
public static void main(String args[]) {
Formatter fmt = new Formatter();
fmt.format("%#o", 1);
System.out.println(fmt);
}
}
The output: