The java.text
package contains a single concrete
subclass of NumberFormat
,
DecimalFormat
. The
DecimalFormat
class provides even more control
over how floating point numbers are formatted:
public class DecimalFormat extends NumberFormat
Most number formats are in fact decimal formats. Generally, you can simply cast any number format to a decimal format, like this:
DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance();
At least in theory, you might encounter a nondecimal format.
Therefore, you should use instanceof
to test
whether or not you’ve got a DecimalFormat
:
NumberFormat nf = NumberFormat.getCurrencyInstance(); if (nf instanceof DecimalFormat) { DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(); //... }
Alternately, you can place the cast and associated operations in a
try
/catch
block that catches
ClassCastException
s:
try { DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(); //... } catch (ClassCastException e) {System.err.println(e);}
Every
DecimalFormat
object has a pattern that describes how
numbers are formatted and a list of symbols that describes with which
characters they’re formatted. This allows the single
DecimalFormat
class to be parameterized so that it
can handle many different formats for different kinds of numbers in
many locales. The pattern is given as an ASCII string. The symbols
are provided by a DecimalFormatSymbols
object.
These are accessed and manipulated through the following six methods:
public DecimalFormatSymbols getDecimalFormatSymbols() public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols) public String toPattern() public String toLocalizedPattern() public void applyPattern(String pattern) public void applyLocalizedPattern(String pattern)
The decimal format symbols specify the characters or strings used for
the zero digit, the grouping separator, the decimal sign, the percent
sign, the mille percent sign, infinity (IEEE 754 Inf), not a number
(IEEE 754 NaN), and the minus sign. In American English these are
0
, ,
, .
,
%
, ‰, Inf
,
NaN
, and -
, respectively. They
may be other things in different locales.
The pattern
specifies whether leading and trailing
zeros are to be printed, whether the fractional part of the number is
printed, the number of digits in a group (three in American English),
and the leading and trailing suffixes for negative and positive
numbers. Patterns are described using an almost Backus-Naur Form
(BNF) grammar, given here:
pattern -> subpattern{;subpattern} subpattern -> {prefix}integer{.fraction}{suffix} prefix -> '\u0000'..'\uFFFD' - specialCharacters suffix -> '\u0000'..'\uFFFD' - specialCharacters integer -> '#'* '0'* '0' fraction -> '0'* '#'*
The first line is not pure BNF. The first subpattern is used for positive numbers. The second subpattern, which may not be present, is used for negative numbers. If it’s not present, negative numbers use the positive format but are prefixed with a minus sign. Table 16.2 defines the symbols used in the grammar.
Table 16-2. Symbols Used in Decimal Format Patterns
This results in patterns like those seen in Table 16.3 for various locales. For instance, #,##0.### is the decimal format pattern for U.S. English and most other non-Arabic-speaking locales. The # mark means any digit character except a leading or trailing zero. The comma is the grouping separator, the period is the decimal point separator, and the is a digit that will be printed even if it’s a nonsignificant zero. You interpret this pattern as follows:
The integer part contains as many digits as necessary.
These are separated every three digits with the grouping separator.
If the integer part contains only zeros, there is a single zero before the decimal separator.
Up to three digits are printed after the decimal separator. However, they are not printed if they are trailing zeros.
No separate pattern is included for negative numbers. Therefore, they will be printed the same as a positive number but prefixed with a minus sign.
It’s relatively painful to work with this grammar directly. Fortunately, there are methods that allow you to get and set the values of these individual pieces directly, and I recommend that you use them:
public String getPositivePrefix() public void setPositivePrefix(String newValue) public String getPositiveSuffix() public void setPositiveSuffix(String newValue) public String getNegativePrefix() public void setNegativePrefix(String newValue) public String getNegativeSuffix() public void setNegativeSuffix(String newValue) public int getMultiplier() public void setMultiplier(int newValue) public int getGroupingSize() public void setGroupingSize(int newValue) public boolean isDecimalSeparatorAlwaysShown() public void setDecimalSeparatorAlwaysShown(boolean newValue)
I can only guess why the patterns and the decimal format symbols
themselves are exposed as much as they are in the
java.text
package. In my opinion, this is poor
design, since it ties the interface too closely to the implementation
of the class. The get
and set
methods are fully adequate for manipulating the formatting. Since you
probably won’t be setting a pattern more than once or twice in
a program (how many different formats does one program need?),
there’s no significant performance gain by using a pattern
instead of the get
and set
methods. And there is a significant cost in complexity. Allowing
direct setting of patterns requires the class to check that the
patterns are valid and throw a ParseException
if
they’re not. The only advantage I can see to manipulating
number formats as pattern strings is that it makes them easy to store
in resource bundles. However, I’m not sure this ability really
needs to be public.
The positive prefix is the string prefixed to positive numbers. Most of the time this is the empty string “”, but in some circumstances you might want to use a plus sign (+). In currency formats, the positive prefix is often set to the currency sign, like $ or £, depending on the locale. You can also set a positive suffix; that is, a string that is appended to all positive numbers. I’m not aware of any number formats that use positive suffixes, but if you need to, you can. The negative prefix is the minus sign (-). However, in accounting and other financial applications it may be an open parenthesis instead. In these applications, there’s also a negative suffix, generally a closing parenthesis. Thus, -12 might be formatted as (12).
The multiplier is an integer by which the number is multiplied before being formatted. This is commonly used in percent formats. This allows a number like 0.85 to be formatted as 85% instead of 0.85%. 1, 100, and 1000 are the only common values of this number. Grouping size is the number of digits between grouping separators, commas in English. This is how 75365 becomes 75,365. Most locales, including English, break every three digits; a few break every four, formatting 75365 as 7,5365. Finally, you can specify whether or not the decimal separator (decimal point) is shown in numbers without fractional parts. By default, a number like 1999 does not have a decimal point. However, there are situations (C source code, for example) where the difference between 1999 and 1999. is significant.
You also have access to the following methods, inherited from
java.text.NumberFormat
, which allow you to set and
get the minimum and maximum number of integer and fraction digits and
control whether or not grouping is used at all. These work just as
well with decimal formats as they do with regular number formats.
public boolean isGroupingUsed() public void setGroupingUsed(boolean useGrouping) public int getMaximumIntegerDigits() public void setMaximumIntegerDigits(int maxDigits) public int getMinimumIntegerDigits() public void setMinimumIntegerDigits(int minDigits) public int getMaximumFractionDigits() public void setMaximumFractionDigits(int maxDigits) public int getMinimumFractionDigits() public void setMinimumFractionDigits(int minDigits)
Table 16.3 lists the default patterns used by different locales’ decimal formats. For the basic decimal format shown in columns 2 and 3, most of the locales use the same pattern as the U.S. English locale. The notable exceptions are the Arabic-speaking countries and Macedonia. The primary difference between locales comes in the decimal format symbols, not the pattern. The percent formats, (not shown) all round down to the nearest integer. With one exception, all locales share the percent format #,##0%. The one exception is the #,##0‰ used in the mainland China locale. This uses a per mille (per thousand), as opposed to the more common percent (per hundred).
The currency formats, shown in columns 4 and 5, are a lot more
interesting, because most countries have their own currencies with
their own unique symbols. Even when countries share a symbol and a
name for the currency, such as the dollar ($), it’s still
important to distinguish between Canadian, American, and Australian
dollars. Many of the currencies in Table 16.3,
especially for country-independent language locales, use the ¤
symbol, which refers to a currency of indeterminate type. The [RLM]
you’ll see in many of the Arabic formats stands for the non-printing
Unicode character u200f
, the right-to-left
marker, used to ensure proper directionality in the number in
Arabic’s right-to-left system. Hebrew is also a right-to-left
script, but in modern Hebrew numbers are generally written in the
European fashion from left to right.
Table 16-3. Decimal Format Patterns
Language (Country) |
Decimal Pattern |
Example:-1234.56 |
Currency Pattern |
Example: -1,234.56 |
---|---|---|---|---|
Albanian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Albanian (Albania) |
#,##0.### |
-1.234,56 |
Lek#,##0.### |
-Lek1.234,56 |
Arabic |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
Arabic (Algeria) |
#,##0.###; #, ##0.###- |
1,234.56- | ||
Arabic (Bahrain) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Egypt) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Iraq) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Jordan) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Kuwait) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Lebanon) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Libyan Arab Jamahiriya) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Morocco) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Oman) |
#,##0.###;#, ##0.###- |
1,234.56- | ||
Arabic (Qatar) |
#,##0.###; #,##0.###- |
1,234.56- | ||
Arabic (Saudi Arabia) |
#,##0.###; #,##0.###- |
1,234.56- | ||
Arabic (Sudan) |
#,##0.###; #,##0.###- |
1,234.56- | ||
Arabic (Syria) |
#,##0.###; #,##0.###- |
1,234.56- | ||
Arabic (Tunisia) |
#,##0.###; #,##0.###- |
1,234.56- | ||
Arabic (United Arab Emirates) |
#,##0.###; #,##0.###- |
1,234.56- | ||
Arabic (Yemen) |
#,##0.###; #,##0.###- |
1,234.56- |
[RLM] 1,234.56- | |
Bulgarian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Bulgarian (Bulgaria) |
#,##0.### |
-1 234,56 | ||
Byelorussian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Byelorussian (Belarus) |
#,##0.### |
-1 234,56 |
#,##0.## |
- 1 234,56 |
Catalan |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Catalan (Spain) |
#,##0.### |
-1.234,56 |
Pts #,##0 |
-Pts 1.235 |
Chinese |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
Chinese (China) |
#,##0.### |
-1,234.56 |
¥#,##0.00 |
-¥1,234.56 |
Chinese (Taiwan) |
#,##0.### |
-1,234.56 |
NT$#,##0.00 |
-NT$1,234.56 |
Croatian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Croatian (Croatia) |
#,##0.### |
-1.234,56 |
Kn #,##0.## |
-Kn 1.234,56 |
Czech |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Czech (Czech Republic) |
#,##0.### |
-1.234,56 | ||
Danish |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Danish (Denmark) |
#,##0.### |
-1.234,56 |
kr #,##0.00; kr -#,##0.00 |
kr -1.234,56 |
Dutch |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Dutch (Belgium) |
#,##0.### |
-1.234,56 |
#,##0.00 BF |
-1.234,56 BF |
Dutch (Netherlands) |
#,##0.### |
-1.234,56 |
fl #,##0.00; fl #,##0.00- |
fl 1.234,56- |
English |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
English (Australia) |
#,##0.### |
-1,234.56 |
$#,##0.00 |
-$1,234.56 |
English (Canada) |
#,##0.### |
-1,234.56 |
$#,##0.00 |
-$1,234.56 |
English (Ireland) |
#,##0.### |
-1,234.56 |
IR£#,##0.00 |
-IR£1,234.56 |
English (New Zealand) |
#,##0.### |
-1,234.56 |
$#,##0.00 |
-$1,234.56 |
English (South Africa) |
#,##0.### |
-1,234.56 |
R #,##0.00; R-#,##0.00 |
R-1,234.56 |
English (United Kingdom) |
#,##0.### |
-1,234.56 |
£#,##0.00 |
-£1,234.56 |
English (United States) |
#,##0.### |
-1,234.56 |
$#,##0.00; ($#,##0.00) |
($1,234.56) |
Estonian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Estonian (Estonia) |
#,##0.### |
-1 234,56 |
#,##0.## kr |
-1 234,56 kr |
Finnish |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Finnish (Finland) |
#,##0.### |
-1 234,56 |
#,##0.00 mk |
-1 234,56 mk |
French |
#,##0.### |
-1,234,56 |
¤ #,##0.00 |
-¤ 1,234,56 |
French (Belgium) |
#,##0.### |
-1.234,56 |
#,##0.00 FB |
-1.234,56 FB |
French (Canada) |
#,##0.### |
-1 234,56 |
#,##0.00 $; (#,##0.00$) |
(1 234,56$) |
French (France) |
#,##0.### |
-1 234,56 |
#,##0.00 F |
-1 234,56 F |
French (Switzerland) |
#,##0.### |
-1’234.56 |
SFr. #,##0.00; SFr.-#,##0.00 |
SFr.-1’234.56 |
German |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
German (Austria) |
#,##0.### |
-1.234,56 |
öS #,##0.00 |
- öS 1.234,56 |
German (Germany) |
#,##0.### |
-1.234,56 |
#,##0.00 DM |
-1.234,56 DM |
German (Switzerland) |
#,##0.### |
-1’234.56 |
SFr. #,##0.00; SFr.-#,##0.00 |
SFr.-1’234.56 |
Greek |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Greek (Greece) |
#,##0.### |
-1.234,56 | ||
Hebrew |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
Hebrew (Israel) |
#,##0.### |
-1,234.56 | ||
Hungarian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Hungarian (Hungary) |
#,##0.### |
-1 234,56 |
Ft#,##0.## |
-Ft1 234,56 |
Icelandic |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Icelandic (Iceland) |
#,##0.### |
-1.234,56 |
#,##0.## kr. |
-1.234,56 kr. |
Italian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Italian (Italy) |
#,##0.### |
-1.234,56 |
L. #,##0 |
-L. 1.235 |
Italian (Switzerland) |
#,##0.### |
-1’234.56 |
SFr. #,##0.00; SFr.-#,##0.00 |
SFr.-1’234.56 |
Japanese |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
Japanese (Japan) |
#,##0.### |
-1,234.56 |
¥#,##0.00 |
-¥1,234.56 |
Korean |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
Korean (South Korea) |
#,##0.### |
-1,234.56 | ||
Latvian (Lettish) |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Latvian (Lettish) (Latvia) |
#,##0.### |
-1 234,56 |
#,##0.## Ls |
-1 234,56 Ls |
Lithuanian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Lithuanian (Lithuania) |
#,##0.## |
-1.234,56 |
#,##0.## Lt |
-1.234,56 Lt |
Macedonian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Macedonian (Macedonia) |
#,##0.###; (#,##0.###) |
(1.234,56) |
Den #,##0.## |
-Den 1.234,56 |
Norwegian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Norwegian (Norway) |
#,##0.### |
-1 234,56 |
kr #,##0.00; kr -#,##0.00 |
kr -1 234,56 |
Norwegian (Norway) |
#,##0.### |
-1 234,56 |
kr #,##0.00; kr -#,##0.00 |
kr -1 234,56 |
Polish |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Polish (Poland) |
#,##0.### |
-1 234,56 | ||
Portuguese |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Portuguese (Brazil) |
#,##0.### |
-1.234,56 |
R$ #,##0.## |
-R$ 1.234,56 |
Portuguese (Portugal) |
#,##0.### |
-1.234,56 |
#,##0.00 Esc. |
-1.234,56 Esc. |
Romanian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Romanian (Romania) |
#,##0.### |
-1.234,56 |
#,##0.00 LEI |
-1.234,56 LEI |
Russian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Russian (Russian Federation) |
#,##0.### |
-1 234,56 | ||
Serbian |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Serbian (Yugoslavia) |
#,##0.### |
-1 234,56 | ||
Serbo-Croatian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Serbo-Croatian (Yugoslavia) |
#,##0.### |
-1.234,56 |
Din #,##0.00 |
-Din 1.234,56 |
Slovak |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Slovak (Slovakia) |
#,##0.### |
-1 234,56 |
Sk #,##0.00 ; -#,##0.00 Sk |
-1 234,56 Sk |
Slovenian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Slovenian (Slovenia) |
#,##0.### |
-1.234,56 |
tol #,##0.## |
-tol 1.234,56 |
Spanish |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Spanish (Argentina) |
#,##0.### |
-1.234,56 |
$#,##0.00; ($#,##0.00) |
($1.234,56) |
Spanish (Bolivia) |
#,##0.### |
-1,234.56 |
B$#,##0.00; (B$#,##0.00) |
(B$1,234.56) |
Spanish (Chile) |
#,##0.### |
-1.234,56 |
Ch$#,##0.00; Ch$-#,##0.00 |
Ch$-1.234,56 |
Spanish (Colombia) |
#,##0.### |
-1,234.56 |
C$#,##0.00; (C$#,##0.00) |
(C$1,234.56) |
Spanish (Costa Rica) |
#,##0.### |
-1,234.56 |
C#,##0.00; (C#,##0.00) |
(C1,234.56) |
Spanish (Dominican Republic) |
#,##0.### |
-1,234.56 |
RD$#,##0.00; (RD$#,##0.00) |
(RD$1,234.56) |
Spanish (Ecuador) |
#,##0.### |
-1,234.56 |
S/#,##0.00; S/-#,##0.00 |
S/-1,234.56 |
Spanish (El Salvador) |
#,##0.### |
-1,234.56 |
C#,##0.00; (C#,##0.00) |
(C1,234.56) |
Spanish (Guatemala) |
#,##0.### |
-1,234.56 |
Q#,##0.00; (Q#,##0.00) |
(Q1,234.56) |
Spanish (Honduras) |
#,##0.### |
-1,234.56 |
L#,##0.00; (L#,##0.00) |
(L1,234.56) |
Spanish (Mexico) |
#,##0.### |
-1,234.56 |
$#,##0.00; ($#,##0.00) |
($1,234.56) |
Spanish (Nicaragua) |
#,##0.### |
-1,234.56 |
$C#,##0.00; ($C#,##0.00) |
($C1,234.56) |
Spanish (Panama) |
#,##0.### |
-1,234.56 |
B#,##0.00; (B#,##0.00) |
(B1,234.56) |
Spanish (Paraguay) |
#,##0.### |
-1.234,56 |
G#,##0.00; (G#,##0.00) |
(G1.234,56) |
Spanish (Peru) |
#,##0.### |
-1.234,56 |
S/#,##0.00; S/-#,##0.00 |
S/-1.234,56 |
Spanish (Puerto Rico) |
#,##0.### |
-1,234.56 |
$#,##0.00; ($#,##0.00) |
($1,234.56) |
Spanish (Spain) |
#,##0.### |
-1.234,56 |
#,##0.00 Pts |
-1.234,56 Pts |
Spanish (Uruguay) |
#,##0.### |
-1.234,56 |
NU$ #,##0.00; (NU$#,##0.00) |
(NU$1.234,56) |
Spanish (Venezuela) |
#,##0.### |
-1.234,56 |
Bs#,##0.00; Bs -#,##0.00 |
Bs -1.234,56 |
Swedish |
#,##0.### |
-1 234,56 |
¤ #,##0.00 |
-¤ 1 234,56 |
Swedish (Sweden) |
#,##0.### |
-1 234,56 |
#,##0.00 kr |
-1 234,56 kr |
Thai |
#,##0.### |
-1,234.56 |
¤ #,##0.00 |
-¤ 1,234.56 |
Thai (Thailand) |
#,##0.### |
-1,234.56 |
#,##0.00; -#,##0.00 |
-1,234.56 |
Turkish |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Turkish (Turkey) |
#,##0.### |
-1.234,56 |
#,##0.00 TL |
-1.234,56 TL |
Ukrainian |
#,##0.### |
-1.234,56 |
¤ #,##0.00 |
-¤ 1.234,56 |
Ukrainian (Ukraine) |
#,##0.### |
Each DecimalFormat
object has a
DecimalFormatSymbols
object that contains a list of the
different symbols used by decimal number formats in a particular
locale. The decimal format symbols specify the characters or strings
used for the zero digit, the grouping separator, the decimal sign,
the percent sign, the mille percent sign, infinity (IEEE 754 Inf),
not a number (IEEE 754 NaN), and the minus sign.
DecimalFormatSymbols
has two constructors, but
they’re rarely used:
public DecimalFormatSymbols() public DecimalFormatSymbols(Locale locale)
Instead, the DecimalFormatSymbols
object is
retrieved from a particular DecimalFormat
object
using its getDecimalFormatSymbols()
method:
public DecimalFormatSymbols getDecimalFormatSymbols()
If you create your own DecimalFormatSymbols
object, perhaps for a locale Java doesn’t support, you can make
a DecimalFormat
use it by passing it to
DecimalFormat
’s
setDecimalFormatSymbols()
method:
public void setDecimalFormatSymbols(DecimalFormatSymbols newSymbols)
The DecimalFormatSymbols
class contains mostly
get
and set
methods for
inspecting and setting the values of the different symbols:
public char getZeroDigit() public void setZeroDigit(char zeroDigit) public char getGroupingSeparator() public void setGroupingSeparator(char groupingSeparator) public char getDecimalSeparator() public void setDecimalSeparator(char decimalSeparator) public char getPercent() public void setPercent(char percent) public char getPerMill() public void setPerMill(char perMill) public String getInfinity() public void setInfinity(String infinity) public String getNaN() public void setNaN(String NaN) public char getMinusSign() public void setMinusSign(char minusSign)
The zero digit is the character used for zero. This is
in most Western languages but is different in Arabic and a few other
locales. The grouping separator is the character used to split
groups; a comma is used in the U.S., but a period is used in some
other countries that use a comma as the decimal separator. The
decimal separator is a decimal point (a period) in English but a
comma in some other locales. The percent and per mille characters are
% and ‰ in English, occasionally other things in other
locales. The infinity and not-a-number strings are rarely changed.
They’re Inf
and NaN
as
specified by IEEE 754, generally even in non-English languages like
German, where the word for infinity is Unbegrenztheit and “not
a number” translates as “nicht eine Zahl.” Finally,
the minus sign is the default character used for negative numbers
that do not have a specific prefix. It’s a hyphen (-) in
English. This character is not used if the associated pattern has set
a negative prefix.
The DecimalFormatSymbols
class also lets you set
two of the characters used in patterns. The digit character, # by
default, and the pattern separator character, a semicolon by default,
can be changed if you want to use one of those characters as a
literal in the pattern; for example, by using a # instead of a period
for the decimal separator.
public char getDigit() public void setDigit(char digit) public char getPatternSeparator() public void setPatternSeparator(char patternSeparator)
The pattern separator is used to separate positive from negative patterns. If no explicit negative pattern is given, then a minus sign is simply prefixed onto the positive pattern instead.
For completeness, I’ll note that
DecimalFormatSymbols
overrides the usual three utility methods from
java.lang.Object
, hashCode()
,
clone()
, and equals()
:
public int hashCode() public boolean equals(Object obj) public Object clone()
One DecimalFormatSymbols
object is equal to
another DecimalFormatSymbols
object if
they’re instances of the same class and all their symbols are
the same.
Most of the time, you use the factory
methods in NumberFormat
to get
DecimalFormat
instances. However, there are three
public DecimalFormat
constructors you can use to
create DecimalFormat
instances directly:
public DecimalFormat() public DecimalFormat(String pattern) public DecimalFormat(String pattern, DecimalFormatSymbols symbols)
The no-argument constructor creates a decimal format that uses the default pattern and symbols for the default locale. The second constructor creates a decimal format that uses the specified pattern and the default symbols for the default locale. The third constructor creates a decimal format that uses the specified pattern and the specified symbols for the default locale. These are useful for special cases that aren’t handled by the default patterns and symbols.
18.226.93.137