Format Statement

The format() statement is used to construct a string based on the template and parameters provided.

The following is a general example of using the format statement:

$num = 7

$date = "Jul 3, 2006 12:30:56 PM"

$string = "a power failure"

 

$msg =  format("At {1,time} on {1,date}, there was {2} in Building {0,number,integer}.",$num , $date, $string);

output: At 12:30 PM on Jul 3, 2006, there was a power failure in Building 7.

The format statement uses the same syntax and patterns as the MessageFormat Java class.

Dates and times

When passing dates into the format statement, two date formats are allowed: fixed and full. The following code sample illustrates how the script checks for date formats:

List<DateFormat> formats = new ArrayList<DateFormat>();
formats.add(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL));
formats.add(new SimpleDateFormat("MMM dd, yyyy HH:mm:ss aa"));

Full format

The full date format is based on the User's locale and the precise formatting will change accordingly. The following table illustrates how the full format date for Tuesday, January 16, 2007 would appear for Users in different locales:

Locale

Full Format Date

United States

Tuesday, January 16, 2007

United Kingdom

16 January 2007

Germany

Dienstag, 16. Januar 2007

France

mardi 16 janvier 2007

Fixed format

The fixed format is based on the following pattern letters:

MMM dd, yyyy HH:mm:ss aa

For an explanation of the pattern letters used in the scripts, see Formatting dates and times.

Examples

The following examples illustrate some methods of formatting dates:

$date = "Feb 27, 2002 2:00:00 PM"

$msg = format("Today is {0}", $date)

// Today is 2/27/02 2:00 PM

 

$msg = format("Today is {0,date}", $date)

// Today is Feb 27, 2002

 

$msg = format("Today is {0,date,short}", $date)

// Today is 2/27/02

 

$msg = format("Today is {0,date,medium}", $date)

// Today is Feb 27, 2002

 

$msg = format("Today is {0,date,long}", $date)

// Today is February 27, 2002

 

$msg = format("Today is {0,date,full}", $date)

// Today is Wednesday, February 27, 2002

 

// Use a custom format

$msg = format("Today is {0,date,MMM d}", $date)

// Today is Feb 27

The following examples illustrate some methods of formatting times:

$date = "Feb 27, 2002 2:08:48 PM"

$msg = format("The time is {0}", $date)

// The time is 2/27/02 2:08 PM

 

$msg = format("The time is {0,time}", $date)

// The time is 2:08:48 PM

 

$msg = format("The time is {0,time,short}", $date)

// The time is 2:08 PM

 

$msg = format("The time is {0,time,medium}", $date)

// The time is 2:08:48 PM

 

$msg = format("The time is {0,time,long}", $date)

// The time is 2:08:48 PM PST

 

$msg = format("The time is {0,time,full}", $date)

// The time is 2:08:48 PM PST

 

$msg = format("The time is {0,time,HH-mm-ss}", $date)

// The time is 14-11-41

Numbers

The following examples illustrate some methods of formatting numbers:

$num1 = 123

$num2 = 1234

$msg = format("There are {0} a''s and {1} b''s", $num1, $num2);

// There are 123 a's and 1,234 b's

 

$msg = format("There are {0,number} a''s and {1,number} b''s ", $num1, $num2);

// There are 123 a's and 1,234 b's

 

// Floating point numbers

$float1 = 123.45

$float2 = 1234.56

 

$msg = format("There are {0,number,#.#} a''s and {1,number,#.#} b''s", $num1, $num2);

// There are 123.4 a's and 1234.6 b's

 

// Currency

$msg = format("There are {0,number,currency} a''s and {1,number,currency} b''s", $num1, $num2);

// There are $123.00 a's and $1,234.00 b's

 

// Percent

$msg = format("There are {0,number,percent} a''s and {1,number,percent} b''s", $num1, $num2);

// There are 12,345% a's and 123,456% b's

 

Flow Control