Table of Contents
Customizing Layouts
You can use #### the DecimalFormat
class to format decimal numbers into specific local strings. This class allows you to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. If you want to change the format symbols, such as the decimal separator, you can use DecimalFormatSymbols
with the DecimalFormat
class. These classes provide a lot of flexibility in number formatting, but they can make your code more complex.
The following text uses #### examples that illustrate the categories DecimalFormat
and DecimalFormatSymbols
. The code examples in this article are taken from a sample program called DecimalFormatDemo
.
build patterns ####
You specify the formatting properties of a DecimalFormat
style String
. The pattern defines the shape of the formatted number. For a full description of the style syntax, see Number Formatting Style Syntax .
The following example creates a formatter by passing a style String
to the DecimalFormat
constructor. The format
method accepts the double
value as an argument and returns a initialized number in String
:
DecimalFormat myFormatter = new decimal format (style); String output = myFormatter.format(value); System.out.println(value + "" + style + "" + output);
The output of the previous lines of code is described in the following table. This value
is the number double
to be formatted. And pattern
it is String
that determines the characteristics of coordination. It represents output
, which is of a String
, number coordinator.
value | pattern | output | Explanation |
---|---|---|---|
123456.789 | ####, ####. #### | 123456.789 | The pound sign (####) indicates a number, the comma is a placeholder for the grouping separator, and the period is a placeholder for the decimal separator. |
123456.789 | ####. ## | 123456.79 | The number has value three digits to the right of the decimal point, but the number pattern has only two. On the format method of addressing this arrest. |
123.78 | 000000.000 | 000123.780 | It pattern specifies the principal behind zeros, because the 0 character is used instead of the pound (#). |
12345.67 | $ ####, ####. #### | 12,345.67 USD | The first letter in pattern the dollar sign ($). Note that it immediately precedes the leftmost number in the output . |
12345.67 | \u00A5 ####, ####. #### | South Korean Won | The pattern Japanese yen (¥) currency sign with 00A5 specifies the Unicode value. |
Locale sensitive format ####
The previous example created DecimalFormat
an object for the default Locale
. If you want DecimalFormat
a non-default object Locale
, you can instantiate it NumberFormat
and then send it to DecimalFormat
. This is an example:
NumberFormat nf = NumberFormat.getNumberInstance(loc), DecimalFormat df = (DecimalFormat) nf; df.applyPattern(pattern); String output = df.format(value); System.out.println(style + "" + output + "" + loc.toString());
Running the previous code example leads to the following output. The formatted number in the second column varies depending on Locale
:
####, ####. #### 123,456.789 en_US ###, ###. ### 123.456,789 de_DE ###, ###. ### 123 456,789 fr_FR
So far, the formatting patterns discussed here follow American English conventions. For example, in the style ###, ###. ## The comma is the thousands separator and the point represents the decimal point. This Agreement is fine, provided that End Users do not compromise it. However, some applications, such as spreadsheets and report builder, allow end users to define their own formatting styles. For these applications, the format patterns defined by the end users must use local notation. In these cases, you’ll want to call the applyLocalizedPattern
method on the DecimalFormat
object.
Edit formatting codes
You can use the DecimalFormatSymbols class to change the symbols that appear in the formatted numbers that the format
method produces . These symbols include the decimal separator, the aggregation separator, the minus sign, and the percent sign, among others.
The following example illustrates the DecimalFormatSymbols
separation by applying an odd format to a number. The unusual form is the result of calls to setDecimalSeparator
, setGroupingSeparator
and setGroupingSize
methods.
Unusual DecimalFormatSymbols = new DecimalFormatSymbols(currentLocale); unusual unusualSymbols.setGroupingSeparator('^'); String strange = "#, ##0. ####"; DecimalFormat weirdFormatter = new decimal format (weird, unusual, symbols); weirdFormatter.setGroupingSize (4), String bizarre = weirdFormatter.format (12345.678); System.out.println (strange);
When run, this example prints the number in a strange format:
1 ^ 2345 | 678
Number format style formula
You can design your own formatting patterns for numbers by following the rules defined by the following BNF scheme:
pattern: = subpattern {; subpattern} subpattern: = {prefix} integer {. part} {suffix} Prefix: = '\\ u0000' .. '\\ uFFFD' - special characters Suffix: = '\\ u0000' .. '\\ uFFFD' - special characters Integer: = '#' * '0' * '0' break: = '0' * '#' *
The notation used in the previous diagram is explained in the following table:
icons | Describe |
---|---|
X* | 0 or more cases of X |
(X | Y) | Either X or Y |
X..Y | Any letter from X through Y, inclusive |
S - T | Characters in S, except for those in T. |
{X} | X optional |
In the previous BNF diagram, the first subpattern defines the positive number format. The second subpattern, which is optional, defines the negative number format.
Although not observed in the BNF diagram, a comma may appear within the integer part.
Under substyles, you can specify the formatting with special icons. These symbols are described in the following table:
Code | Describe |
---|---|
0 | number |
# | Number, zero appears as absent |
. | placeholder for decimal separator |
And the | placeholder for separator grouping |
e | Separates the decimal part and the exponent from the exponential formats |
; | separates shapes |
– | default negative prefix |
% | Multiply by 100 and display as a percentage |
? | Multiply by 1000 and display by slope |
¤ | The currency sign replaced by the currency symbol; If doubled, replace with the international currency symbol; If it is in a pattern, the monetary decimal separator is used instead of the decimal separator |
X | Any other characters can be used in the prefix or suffix |
“ | Used to quote special characters in a prefix or suffix |