Java Reference
In-Depth Information
{
double
x = 15.674;
double
y = 235.73;
double
z = 9525.9864;
int
num = 83;
String str;
System.out.println("123456789012345678901234567890");
System.out.println(String.format("%.2f", x));
System.out.println(String.format("%.3f", y));
System.out.println(String.format("%.2f", z));
3
System.out.println(String.format("%7s", "Hello"));
System.out.println(String.format("%5d%7.2f", num, x));
System.out.println(String.format("The value of "
+ "num = %5d", num));
str = String.format("%.2f", z);
System.out.println(str);
}
}
Sample Run:
123456789012345678901234567890
15.67
235.730
9525.99
Hello
83 15.67
The value of num
ΒΌ
83
9525.99
The preceding sample run is self-explanatory. The details are left as an exercise for you.
The following example illustrates how the
String
method
format
can be used to format
the output in an output dialog box.
EXAMPLE 3-15
import
javax.swing.JOptionPane;
public class
Example3_15
{
public static void
main(String[] args)
{
double
x = 15.674;
double
y = 235.73;
double
z = 9525.9864;
String str;
Search WWH ::
Custom Search