Summary of Patterns
Home ] Up ] MessageFormat ] Explicit Formats ] [ Summary of Patterns ] Formatting Numbers ]

 

 

Summary of MessageFormat Patterns

Here is the grammar for MessageFormat Patterns:

messageFormatPattern := string ( "{" messageFormatElement "}" string )* 
messageFormatElement := argument { "," elementFormat } 
elementFormat := "time" { "," datetimeStyle }
               | "date" { "," datetimeStyle }
               | "number" { "," numberStyle }
               | "choice" { "," choiceStyle } 
datetimeStyle := "short"
               | "medium"
               | "long"
               | "full"
               | dateFormatPattern 
numberStyle := "currency"
             | "percent"
             | "integer"
             | numberFormatPattern 
choiceStyle := choiceFormatPattern

Notes:

  • If there is no elementFormat, then the argument must be a String, which is substituted.
  • If there is no dateTimeStyle or numberStyle, then the default format is used (for example, NumberFormat.getInstance, DateFormat.getTimeInstance, or DateFormat.getInstance).
  • In strings, single quotes can be used to quote the "{" (curly brace) if necessary. A real single quote is represented by '' (two successive single quotes). Inside a messageFormatElement, quotes are not removed. For example, {1,number,$'#',##} will produce a number format with the pound-sign quoted, with a result such as: "$#31,45".
  • If a pattern is used, then unquoted braces in the pattern, if any, must match: that is, "ab {0} de" and "ab '}' de" are OK, but "ab {0'}' de" and "ab } de" are not.
  • The argument is a number from 0 to 9, which corresponds to the arguments presented in an array to be formatted.
  • It is ok to have unused arguments in the array. With missing arguments or arguments that are not of the right class for the specified format, a ParseException is thrown. First, format checks to see if a Format object has been specified for the argument with the setFormats method. If so, then format uses that Format object to format the argument. Otherwise, the argument is formatted based on the object's type:
    • If the argument is a Number, then format uses NumberFormat.getInstance to format the argument; 
    • if the argument is a Date, then format uses DateFormat.getDateTimeInstance to format the argument.
    • Otherwise, it uses the toString method.

Summary of ChoiceFormat

A ChoiceFormat allows you to attach a format to a range of numbers. It is generally used in a MessageFormat for handling plurals. The choice is specified with an ascending list of doubles, where each item specifies a half-open interval up to the next item:

X matches j if and only if limit[j] <= X < limit[j+1] 

If there is no match, then either the first or last index is used, depending on whether the number (X) is too low or too high.

 

The page was last updated February 19, 2008