FormatterImplpublic class FormatterImpl extends Object implements CommonFormatterThis class actually realizes most of the methods of
{@link javax.microedition.global.Formatter}. Specifically, these are:
- {@link #formatDateTime(Calendar, int)}
- {@link #formatCurrency(double)}
- {@link #formatCurrency(double, String)}
- {@link #formatNumber(double)}
- {@link #formatNumber(double, int)}
- {@link #formatNumber(long)}
- {@link #formatPercentage(long number)}
- {@link #formatPercentage(float, int)}
|
Fields Summary |
---|
private String | localeCurrent FormatterImpl locale code. |
Constructors Summary |
---|
public FormatterImpl(String locale)Constructs a formatter implementation for the specified locale.
int locale_index = LocaleHelpers.indexInSupportedLocales(locale,
Formatter.getSupportedLocales());
if (locale_index == -1) {
throw new UnsupportedLocaleException("Locale \""
+ locale +
"\" unsupported.");
}
this.locale = locale;
|
Methods Summary |
---|
public java.lang.String | formatCurrency(double number)Formats a currency amount using locale-specific rules. This method
assumes that the amount is in the locale's currency. The result uses the
locale-specific decimal separator and may include grouping separators.
The number of decimals is also locale-specific.
This method does not perform any currency conversions based on exchange
rates.
NumberFormat nf = new NumberFormat(NumberFormat.CURRENCYSTYLE,
getNumberFormatSymbols());
if (Double.isInfinite(number) ||
Double.isNaN(number)) {
String oldSymbol = nf.setCurrencySymbol("");
String formatted = nf.format(number);
nf.setCurrencySymbol(oldSymbol);
return formatted;
} else {
return nf.format(number);
}
| public java.lang.String | formatCurrency(double number, java.lang.String currencyCode)Formats a currency amount using the locale-specific rules but using the
symbol of the specified currency. The currency is specified using its
ISO 4217 three-letter code, such as "USD", "EUR" or "GBP". If there is a
currency symbol attached to the ISO 4217 code in the implementation,
that symbol MUST be used instead of the locale's currency symbol. If the
implementation does not provide a currency symbol for a given ISO 4217
code, the code MUST be used as such.
The result uses the locale-specific decimal separator and may include
grouping separators. The number of decimals is also locale-specific.
This method does not perform any currency conversions based on exchange
rates.
NumberFormat nf = new NumberFormat(NumberFormat.CURRENCYSTYLE,
getNumberFormatSymbols());
String symbol = "";
if (!(Double.isInfinite(number) ||
Double.isNaN(number))) {
symbol = nf.getCurrencySymbolForCode(currencyCode);
if (symbol == null) {
symbol = currencyCode;
}
}
String oldSymbol = nf.setCurrencySymbol(symbol);
String formatted = nf.format(number);
nf.setCurrencySymbol(oldSymbol);
return formatted;
| public java.lang.String | formatDateTime(java.util.Calendar dateTime, int style)Formats a date/time instance using locale-specific rules.
The style parameter determines the extent and style
of the result.
If dateTime has an associated
java.util.TimeZone
object, dateTime MUST be interpreted as "wall time", not
universal time. The offset of that time zone MUST be reflected in the
formatted time.
try {
ResourceManager rm = ResourceManager.getManager(
ResourceManager.DEVICE, locale);
DateFormatSymbols symbols = (DateFormatSymbols)rm.getResource(
Constants.DATETIME_FORMAT_SYMBOL_RESOURCE_ID);
DateTimeFormat dtf = new DateTimeFormat(style, symbols);
NumberFormat nf = new NumberFormat(NumberFormat.INTEGERSTYLE,
getNumberFormatSymbols());
return dtf.format(dateTime, nf);
} catch (NullPointerException npe_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.WARNING) {
Logging.report(Logging.WARNING, LogChannels.LC_JSR238,
"Base name or locale is null");
}
} catch (IllegalArgumentException iae_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.WARNING) {
Logging.report(Logging.WARNING, LogChannels.LC_JSR238,
"locale identifier is not valid" + locale);
}
} catch (ResourceException re_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.WARNING) {
Logging.report(Logging.WARNING, LogChannels.LC_JSR238,
"No resources are found or the resource file is invalid");
}
} catch (UnsupportedLocaleException ule_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.WARNING) {
Logging.report(Logging.WARNING, LogChannels.LC_JSR238,
"Locale is not listed in the meta-information file"
+ locale);
}
}
return "";
| public java.lang.String | formatNumber(double number)Formats a decimal number using locale-specific rules. The result
includes a locale-specific decimal separator and may include grouping
separators.
The symbols used for negative and positive infinity and NaN are
implementation-specific. Implementations MAY use the appropriate Unicode
character (U+221F INFINITY) if applicable.
NumberFormat nf = new NumberFormat(NumberFormat.NUMBERSTYLE,
getNumberFormatSymbols());
return nf.format(number);
| public java.lang.String | formatNumber(double number, int decimals)Formats a decimal number using locale-specific rules, with the specified
number of decimals. The result includes a locale-specific decimal
separator and may include grouping separators.
The number of decimals MUST be between 1 and 15. The formatted result
MUST have exactly the specified number of decimals, even if some of the
trailing digits are zeroes.
The symbols used for negative and positive infinity and NaN are
implementation-specific. Implementations MAY use the appropriate Unicode
character (U+221F INFINITY) if applicable.
NumberFormat nf = new NumberFormat(NumberFormat.NUMBERSTYLE,
getNumberFormatSymbols());
nf.setMinimumFractionDigits(decimals);
nf.setMaximumFractionDigits(decimals);
return nf.format(number);
| public java.lang.String | formatNumber(long number)Formats an integer using locale-specific rules. The result may include
grouping separators.
NumberFormat nf = new NumberFormat(NumberFormat.INTEGERSTYLE,
getNumberFormatSymbols());
return nf.format(number);
| public java.lang.String | formatPercentage(float number, int decimals)Formats a percentage with the specified number of decimals using
locale-specific rules. This method places the locale-specific percent
sign at the correct position in relation to the number, with the
appropriate amount of space (possibly none) between the sign and the
number.
A percentage is expressed as a decimal number, with the value 0.0
interpreted as 0% and the value 1.0 as 100%. Percentages larger than
100% are expressed as values greater than 1.0. Negative percentages are
allowed, and expressed as values smaller than 0.0. The percentage is
rounded to the specified number of decimals.
The number of decimals MUST be between 1 and 15. The formatted result
MUST have exactly the specified number of decimals, even if some of the
trailing digits are zeroes.
NumberFormat nf = new NumberFormat(NumberFormat.PERCENTSTYLE,
getNumberFormatSymbols());
nf.setMinimumFractionDigits(1);
nf.setMaximumFractionDigits(decimals);
return nf.format(number);
| public java.lang.String | formatPercentage(long number)Formats an integral percentage value using locale-specific rules. This
method places the locale-specific percent sign at the correct position
in relation to the number, with the appropriate number of space
(possibly none) between the sign and the number.
A percentage is expressed as an integer value. Negative percentages are
allowed.
NumberFormat nf = new NumberFormat(NumberFormat.PERCENTSTYLE,
getNumberFormatSymbols());
nf.setMultiplier(1);
return nf.format(number);
| private NumberFormatSymbols | getNumberFormatSymbols()Creates and returns a set of symbols for number formatting.
try {
ResourceManager rm = ResourceManager.getManager(
ResourceManager.DEVICE, locale);
NumberFormatSymbols symbols = (NumberFormatSymbols)rm.getResource(
Constants.NUMBER_FORMAT_SYMBOL_RESOURCE_ID);
return symbols;
} catch (NullPointerException npe_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.ERROR) {
Logging.report(Logging.ERROR, LogChannels.LC_JSR238,
"Base name or locale is null");
}
} catch (IllegalArgumentException iae_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.ERROR) {
Logging.report(Logging.ERROR, LogChannels.LC_JSR238,
"Locale identifier is not valid" + locale);
}
} catch (ResourceException re_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.ERROR) {
Logging.report(Logging.ERROR, LogChannels.LC_JSR238,
"No resources are found or the resource file is invalid");
}
} catch (UnsupportedLocaleException ule_ignore) {
/* intentionally ignored */
if (Logging.REPORT_LEVEL <= Logging.ERROR) {
Logging.report(Logging.ERROR, LogChannels.LC_JSR238,
"Locale is not listed in the meta-information file"
+ locale);
}
}
if (Logging.REPORT_LEVEL <= Logging.WARNING) {
Logging.report(Logging.WARNING, LogChannels.LC_JSR238,
"Using neutral locale symbols for locale: "
+ locale);
}
return new NumberFormatSymbols();
|
|