System.out.printf("PI =%5.2f%n", Math.PI);
System.out.printf("PI =%5.3f%n", Math.PI);
System.out.printf("PI =%10.1f%n", Math.PI);
PI = 3.14 PI =3.142 PI = 3.1
The last digit was rounded up in the second output.
public class FloatDemoA
{
public static void main ( String[] args )
{
double first = 12345.6789 ;
double second = -45.97 ;
System.out.printf( "first = %10.3f; second = %10.3f; sum = %f", first, second, first+second );
}
}
The format string in this program has three format specifiers. Each matches a value in the argument list. The conversion code in the specifier ("f" here) must match the type of value.
The output (on my computer in the US) is:
first = 12345.679; second = -45.970; sum = 12299.708900
Notes:
What do you suspect the following prints:
double value = 123456.120001;
System.out.printf("value =%5.2f", value);