Java Example – String formatting
The following example demonstrates formatting strings with the format() method; a locale can also be specified for formatting:
StringFormat.java file
import
java
.
util
.*;
public
class
StringFormat
{
public
static
void
main
(
String
[
]
args
)
{
double
e
=
Math
.
E
;
System
.
out
.
format
(
"
%f%n
"
,
e
)
;
System
.
out
.
format
(
Locale
.
CHINA
,
"
%-10.4f%n%n
"
,
e
)
;
//
Specify the locale as China (CHINA)
}
}
The output of the above code example is:
2.718282
2.7183
Java Examples