Richard F.L.R.Snashall said something like:
This is where I started to get lost. The manual page says the method is
"static NumberFormat getInstance( )". Does that mean that the same
static value is always passed back? If so, how do I then make my own
copies? In the example I gave, I used four digits, but I also needed
other numbers of digits.
No, no, the format itself that you're using changes as you specify it.
Ok, here's the way it was designed, which I'm not particularly thrilled
with, but it's "eh, ok".
NumberFormat is an abstract class. It contains the factory methods for
instantiating it's various subclasses.
The various getInstance() methods defined with NumberFormat (see some below)
all call a private getInstance() which takes the desired locale and format
style (see below as well). The private getInstance() employs a cache of
number patterns. Don't ask me why this was considered so critical: I can't
imagine that any of this would have been trouble without the cache.
I would avoid direct instantiation of the various number formats, however,
only for maintainability reasons. Most coders are used to seeing one of the
NumberFormat factory methods used...
If I may, another question; why not getNumberInstance, since my
application is numeric only.
More factory method hooey. Just getInstance() will be fine, it is
equivalent.
Here, this is what the code actually looks like for those factory methods
within NumberFormat (jdk 5.0). Note that NUMBERFORMAT is one of the "type"
constants:
<elsewhere in NumberFormat.java>
// Constants used by factory methods to specify a style of format.
private static final int NUMBERSTYLE = 0;
private static final int CURRENCYSTYLE = 1;
private static final int PERCENTSTYLE = 2;
private static final int SCIENTIFICSTYLE = 3;
private static final int INTEGERSTYLE = 4;
</elsewhere>
<factory methods>
/**
* Returns a general-purpose number format for the current default
locale.
* This is the same as calling
* {@link #getNumberInstance() getNumberInstance()}.
*/
public final static NumberFormat getInstance() {
return getInstance(Locale.getDefault(), NUMBERSTYLE);
}
/**
* Returns a general-purpose number format for the specified locale.
* This is the same as calling
* {@link #getNumberInstance(java.util.Locale)
getNumberInstance(inLocale)}.
*/
public static NumberFormat getInstance(Locale inLocale) {
return getInstance(inLocale, NUMBERSTYLE);
}
/**
* Returns a general-purpose number format for the current default
locale.
*/
public final static NumberFormat getNumberInstance() {
return getInstance(Locale.getDefault(), NUMBERSTYLE);
}
/**
* Returns a general-purpose number format for the specified locale.
*/
public static NumberFormat getNumberInstance(Locale inLocale) {
return getInstance(inLocale, NUMBERSTYLE);
}
</factory>
...............more hooey..............and here is the private method I was
talking about:
private static NumberFormat getInstance(Locale desiredLocale,
int choice) {
/* try the cache first */
String[] numberPatterns =
(String[])cachedLocaleData.get(desiredLocale);
if (numberPatterns == null) { /* cache miss */
ResourceBundle resource =
LocaleData.getLocaleElements(desiredLocale
);
numberPatterns = resource.getStringArray("NumberPatterns");
/* update cache */
cachedLocaleData.put(desiredLocale, numberPatterns);
}
DecimalFormatSymbols symbols = new
DecimalFormatSymbols(desiredLocale);
int entry = (choice == INTEGERSTYLE) ? NUMBERSTYLE : choice;
DecimalFormat format = new DecimalFormat(numberPatterns[entry],
symbols)
;
if (choice == INTEGERSTYLE) {
format.setMaximumFractionDigits(0);
format.setDecimalSeparatorAlwaysShown(false);
format.setParseIntegerOnly(true);
} else if (choice == CURRENCYSTYLE) {
format.adjustForCurrencyDefaultFractionDigits();
}
return format;
}