Java Reference
In-Depth Information
Embedded expressions within string literals may contain a formatting code that
specifies how the embedded expression should be presented. Consider the
following:
var totalCountMessage = "The total count is {total}";
Now if
total
is an integer, the resulting string will show the decimal number;
but if
total
is a Number, the resulting string will show the number formatted
according to the local locale.
var total = 1000.0;
produces:
The total count is 1000.0
To format an expression, you need a format code within the embedded expres-
sion. This is a percent (
%
) followed by the format codes. The format code is
defined in the
java.util.Formatter
class. Please refer to its JavaDoc page for
more details (
http://java.sun.com/javase/6/docs/api/index.html).
println("Total is {
%f
total}");
// Total is 1000.000000
println("Total is {
%.2f
total}");
// Total is 1000.00
println("Total is {
%5.0f
total}");
// Total is 1000
println("Total is {
%+5.0f
total}");
// Total is +1000
println("Total is {
%,5.0f
total}");
// Total is 1,000
Developer Note:
To include a percent (
%
) character in a string, it needs to be
escaped with another percent (
%%
). For example:
println("%%{percentage}"); // prints %25
To internationalize a string, you must use the “Translate Key” syntax within the
string declaration. To create a translate key, the String assignment starts with
##
(sharp, sharp) combination to indicate that the string is to be translated to the
host locale. The
##
combination is before the leading double or single quote.
Optionally, a key may be specified within square brackets (
[]
). If a key is not
Search WWH ::
Custom Search