All Packages Class Hierarchy This Package Previous Next Index
java.lang.Object
|
+----java.text.Format
|
+----java.text.NumberFormat
|
+----java.text.DecimalFormat
Normally, you get the proper NumberFormat for a specific locale (including the default locale). You may then modify it from there (after testing to make sure it is a DecimalFormat, of course!)
Either the prefixes or the suffixes must be different for the parse to distinguish positive from negative. Parsing will be unreliable if the digits, thousands or decimal separators are the same, or if any of them occur in the prefixes or suffixes.
Special cases:
NaN is formatted as a single character, typically \\uFFFD.
+/-Infinity is formatted as a single character, typically \\u221E, plus the positive and negative pre/suffixes.
Note: this class is designed for common users; for very large or small numbers, use a format that can express exponential values.
Example:
// normally we would have a GUI with a menu for this
Locale[] locales = NumberFormat.getAvailableLocales();
double myNumber = -1234.56;
NumberFormat form;
// just for fun, we print out a number with the locale number, currency
// and percent format for each locale we can.
for (int j = 0; j < 3; ++j) {
System.out.println("FORMAT");
for (int i = 0; i < locales.length; ++i) {
if (locales[i].getCountry().length() == 0) {
// skip language-only
continue;
}
System.out.print(locales[i].getDisplayName());
switch (j) {
default:
form = NumberFormat.getDefault(locales[i]); break;
case 1:
form = NumberFormat.getDefaultCurrency(locales[i]); break;
case 0:
form = NumberFormat.getDefaultPercent(locales[i]); break;
}
System.out.print(": " + ((DecimalFormat)form).getPattern()
+ " -> + form.format(myNumber));
System.out.println(" -> " + form.parse(form.format(myNumber)));
}
}
Format: {prefix}integer{.decimal}{suffix}
prefix any characters other than '#','0', or other digits. suffix ditto integer zero or more '#', followed by 1 or more '0'. ',' can occur inside. decimal zero or more '0', followed by zero or more '#'.Here are the other special characters.
Symbol Meaning Notes
0 a digit
* a digit, zero shows as a star Can't mix 0, *, and _ in same format
_ a digit, zero shows as a space Can't mix 0, *, and _ in same format
# a digit, zero shows as absent
. placeholder for decimal separator
, placeholder for grouping delimiter. Shows the interval to be used.
; separates formats. There are two:
positive and negative.
- if there is no explicit negative sign,
- is prefixed "0.00" -> "0.00;-0.00"
% divide by 100 and show as percentage
X any other characters can be used in
the prefix or suffix
Note : When specifying '0', '*' and '_' in the pattern, '*' takes precedence over '_' and '_' takes precedence over '0'.
This class only handles localized digits where the 10 digits are contiguous in Unicode, from 0 to 9. Other digits sets (such as superscripts) would need a different subclass.
Examples: -123, ($123) (with negative suffix), sFr-123
Examples: -123%, ($123) (with positive suffixes)
Examples: +123, $123, sFr123
Example: 123%
Examples: -123, ($123) (with negative suffix), sFr-123
Examples: 123%
Examples: +123, $123, sFr123
Example: 123%
public DecimalFormat(String pattern,
NumberFormatData data) throws FormatException
public DecimalFormat()
public StringBuffer format(double number,
StringBuffer result,
FormatStatus status)
public StringBuffer format(long number,
StringBuffer result,
FormatStatus status)
public Number parse(String text,
ParseStatus status)
public void setNumberFormatData(NumberFormatData newElements)
public NumberFormatData getNumberFormatData()
public String getPositivePrefix()
Examples: +123, $123, sFr123
public void setPositivePrefix(String newValue)
Examples: +123, $123, sFr123
public String getNegativePrefix()
Examples: -123, ($123) (with negative suffix), sFr-123
public void setNegativePrefix(String newValue)
Examples: -123, ($123) (with negative suffix), sFr-123
public String getPositiveSuffix()
Example: 123%
public void setPositiveSuffix(String newValue)
Example: 123%
public String getNegativeSuffix()
Examples: -123%, ($123) (with positive suffixes)
public void setNegativeSuffix(String newValue)
Examples: 123%
public int getFactor()
Examples: with 100, 1.23 -> "123", and "123" -> 1.23
public void setFactor(int newValue)
Examples: with 100, 1.23 -> "123", and "123" -> 1.23
public int getThousandsInterval()
public void setThousandsInterval(int newValue)
public synchronized Object clone()
public boolean equals(Object obj)
public synchronized int hashCode()
public String getPattern(boolean localized)
public void setPattern(String pattern,
boolean localized) throws FormatException
Example "#,#00.0#" -> 1,234.56
This means a minimum of 2 integer digits, 1 decimal digit, and a maximum of 2 decimal digits.
Example: "#,#00.0#;(#,#00.0#)" for negatives in parantheses.
In negative patterns, the minimum and maximum counts are ignored; these are presumed to be set in the positive pattern.
All Packages Class Hierarchy This Package Previous Next Index