Java printf supports two types of numeric formatting:
The integral number formatting formats the whole numbers.
It can format values of byte, Byte, short, Short, int, Integer, long, Long and BigInteger.
The following list explains the format characters.
d
o
0
. x
or Uppercase variant X
The general syntax for a format specifier for integral number formatting is as follows:
%<argument_index$><flags><width><conversion>
The precision in a format specifier is not applicable to integral number formatting.
The following code demonstrates the use of the 'd' conversion with various flags to format integers:
public class Main { public static void main(String[] args) { // w ww.ja v a2 s. c om System.out.printf("'%d' %n", 1234); System.out.printf("'%6d' %n", 1234); System.out.printf("'%-6d' %n", 1234); System.out.printf("'%06d' %n", 1234); System.out.printf("'%(d' %n", 1234); System.out.printf("'%(d' %n", -1234); System.out.printf("'% d' %n", 1234); System.out.printf("'% d' %n", -1234); System.out.printf("'%+d' %n", 1234); System.out.printf("'%+d' %n", -1234); } }
The code above generates the following result.
When o
and x
are used with a negative argument
of byte, Byte, short, Short, int, Integer, long, and Long data types,
the argument value is first converted to an unsigned number by adding a number 2N to it, where N is the number of bits of the value. The conversions do not transform the negative BigInteger argument.
public class Main { public static void main(String[] args) { byte b1 = 1; byte b2 = -2; System.out.printf("%o\n", b1); System.out.printf("%o", b2); } }
The following snippet of code shows some more examples of 'o' and 'x' conversions for int and BigInteger argument types:
import java.math.BigInteger; /*ww w. j a va 2 s . c o m*/ public class Main { public static void main(String[] args) { System.out.printf("%o %n", 1234); System.out.printf("%o %n", -1234); System.out.printf("%o %n", new BigInteger("1234")); System.out.printf("%o %n", new BigInteger("-1234")); System.out.printf("%x %n", 1234); System.out.printf("%x %n", -1234); System.out.printf("%x %n", new BigInteger("1234")); System.out.printf("%x %n", new BigInteger("-1234")); System.out.printf("%#o %n", 1234); System.out.printf("%#x %n", 1234); System.out.printf("%#o %n", new BigInteger("1234")); System.out.printf("%#x %n", new BigInteger("1234")); } }
The code above generates the following result.
Floating-point number formatting deals a whole number part and a fraction part for a number.
Floating-point number formatting can be applied to format values of float, Float, double, Double, and BigDecimal data types.
The following list contains conversions used for formatting floating-point number.
e
and Uppercase Variant E
g
and Uppercase Variant G
e
or f
conversion.f
conversion.f
a
and Uppercase Variant A
The general syntax for floating-point number formatting is
%<argument_index$><flags><width><.precision><conversion>
The precision has different meanings depend on the conversion character.
The following snippet of code shows how to format floating-point numbers with the default precision, which is 6:
public class Main { public static void main(String[] args) { System.out.printf("%e %n", 10.2); System.out.printf("%f %n", 10.2); System.out.printf("%g %n", 10.2); /*from w ww .j a v a 2 s .c o m*/ System.out.printf("%e %n", 0.000001234); System.out.printf("%f %n", 0.000001234); System.out.printf("%g %n", 0.000001234); System.out.printf("%a %n", 0.000001234); } }
The code above generates the following result.
The following code shows the effects of using width and precision in floating-point number formatting:
public class Main { public static void main(String[] args) { System.out.printf("%.2e %n", 123456.789); System.out.printf("%.2f %n", 123456.789); System.out.printf("%.2g %n", 123456.789); /* www.j ava 2 s . c o m*/ System.out.printf("'%8.2e' %n", 123456.789); System.out.printf("'%8.2f' %n", 123456.789); System.out.printf("'%8.2g' %n", 123456.789); System.out.printf("'%10.2e' %n", 123456.789); System.out.printf("'%10.2f' %n", 123456.789); System.out.printf("'%10.2g' %n", 123456.789); System.out.printf("'%-10.2e' %n", 123456.789); System.out.printf("'%-10.2f' %n", 123456.789); System.out.printf("'%-10.2g' %n", 123456.789); System.out.printf("'%010.2e' %n", 123456.789); System.out.printf("'%010.2f' %n", 123456.789); System.out.printf("'%010.2g' %n", 123456.789); } }
The code above generates the following result.
If the argument value for a floating-point conversion is NaN or Infinity, the output contains the strings "NaN" and "Infinity", respectively.
public class Main { public static void main(String[] args) { System.out.printf("%.2e %n", Double.NaN); System.out.printf("%.2f %n", Double.POSITIVE_INFINITY); System.out.printf("%.2g %n", Double.NEGATIVE_INFINITY); System.out.printf("%(f %n", Double.POSITIVE_INFINITY); System.out.printf("%(f %n", Double.NEGATIVE_INFINITY); } }
The code above generates the following result.