Package mondrian.util
Class Format
java.lang.Object
mondrian.util.Format
Format formats numbers, strings and dates according to the
same specification as Visual Basic's
format() function. This function is described in more detail
here. We
have made the following enhancements to this specification:- if the international currency symbol (¤) occurs in a format string, it is translated to the locale's currency symbol.
- the format string "Currency" is translated to the locale's currency format string. Negative currency values appear in parentheses.
- the string "USD" (abbreviation for U.S. Dollars) may occur in a format string.
One format object can be used to format multiple values, thereby amortizing the time required to parse the format string. Example:
double[] values;
Format format = new Format("##,##0.###;(##,##0.###);;Nil");
for (int i = 0; i < values.length; i++) {
System.out.println("Value #" + i + " is " + format.format(values[i]));
}
Still to be implemented:
- String formatting (fill from left/right)
- Use client's timezone for printing times.
- Author:
- jhyde
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA FormatLocale contains all information necessary to format objects based upon the locale of the end-user. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intMaximum number of entries in the format cache used byget(String, java.util.Locale). -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Format.FormatLocalecreateLocale(char thousandSeparator, char decimalPlaceholder, String dateSeparator, String timeSeparator, String currencySymbol, String currencyFormat, String[] daysOfWeekShort, String[] daysOfWeekLong, String[] monthsShort, String[] monthsLong, Locale locale) Create aFormat.FormatLocaleobject characterized by the given properties.static Format.FormatLocalecreateLocale(Locale locale) static FormatConstructs aFormatin a specific locale, or retrieves one from the cache if one already exists.static Format.FormatLocalegetBestFormatLocale(Locale locale) Returns the bestFormat.FormatLocalefor a givenLocale.static Format.FormatLocalegetFormatLocale(Locale locale) Returns theFormat.FormatLocalewhich precisely matchesLocale, if any, or null if there is none.static StringgetFormatToken(int code) Returns the format token as a string representation which corresponds to a given token code.static List<mondrian.util.Format.Token>static Format.FormatLocaleregisterFormatLocale(Format.FormatLocale formatLocale, Locale locale) Registers aFormat.FormatLocaleto a givenLocale.
-
Field Details
-
CacheLimit
public static final int CacheLimitMaximum number of entries in the format cache used byget(String, java.util.Locale).- See Also:
-
-
Constructor Details
-
Format
Constructs aFormatin a specific locale.- Parameters:
formatString- the format string; see this description for more detailslocale- The locale
-
Format
Constructs aFormatin a specific locale.- Parameters:
formatString- the format string; see this description for more detailslocale- The locale- See Also:
-
-
Method Details
-
getTokenList
-
getFormatToken
Returns the format token as a string representation which corresponds to a given token code.- Parameters:
code- The code of the token to obtain.- Returns:
- The string representation of that token.
-
get
Constructs aFormatin a specific locale, or retrieves one from the cache if one already exists.If the number of entries in the cache exceeds
CacheLimit, replaces the eldest entry in the cache.- Parameters:
formatString- the format string; see this description for more details- Returns:
- format for given format string in given locale
-
createLocale
public static Format.FormatLocale createLocale(char thousandSeparator, char decimalPlaceholder, String dateSeparator, String timeSeparator, String currencySymbol, String currencyFormat, String[] daysOfWeekShort, String[] daysOfWeekLong, String[] monthsShort, String[] monthsLong, Locale locale) Create aFormat.FormatLocaleobject characterized by the given properties.- Parameters:
thousandSeparator- the character used to separate thousands in numbers, or ',' by default. For example, 12345 is '12,345 in English, '12.345 in French.decimalPlaceholder- the character placed between the integer and the fractional part of decimal numbers, or '.' by default. For example, 12.34 is '12.34' in English, '12,34' in French.dateSeparator- the character placed between the year, month and day of a date such as '12/07/2001', or '/' by default.timeSeparator- the character placed between the hour, minute and second value of a time such as '1:23:45 AM', or ':' by default.daysOfWeekShort- Short forms of the days of the week. The array is 1-based, because positionCalendar.SUNDAY(= 1) must hold Sunday, etc. The array must have 8 elements. For example {"", "Sun", "Mon", ..., "Sat"}.daysOfWeekLong- Long forms of the days of the week. The array is 1-based, because positionCalendar.SUNDAYmust hold Sunday, etc. The array must have 8 elements. For example {"", "Sunday", ..., "Saturday"}.monthsShort- Short forms of the months of the year. The array is 0-based, because positionCalendar.JANUARY(= 0) holds January, etc. For example {"Jan", ..., "Dec", ""}.monthsLong- Long forms of the months of the year. The array is 0-based, because positionCalendar.JANUARY(= 0) holds January, etc. For example {"January", ..., "December", ""}.locale- if this is not null, register that the constructedFormatLocaleis the default forlocale
-
createLocale
-
getFormatLocale
Returns theFormat.FormatLocalewhich precisely matchesLocale, if any, or null if there is none. -
getBestFormatLocale
Returns the bestFormat.FormatLocalefor a givenLocale. Never returns null, even iflocaleis null. -
registerFormatLocale
public static Format.FormatLocale registerFormatLocale(Format.FormatLocale formatLocale, Locale locale) Registers aFormat.FormatLocaleto a givenLocale. Returns the previous mapping. -
format
-
getFormatString
-