Customizing Layouts

You can use #### the  DecimalFormatclass 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  DecimalFormatSymbolswith the  DecimalFormatclass. 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  DecimalFormatand  DecimalFormatSymbols. The code examples in this article are taken from a sample program called  DecimalFormatDemo.

build patterns ####

You specify the formatting properties of a  DecimalFormatstyle  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  Stringto the  DecimalFormatconstructor. The  formatmethod accepts the  doublevalue 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  valueis the number  doubleto be formatted. And  patternit is  Stringthat determines the characteristics of coordination. It represents  output, which is of a  String, number coordinator.

valuepatternoutputExplanation
123456.789####, ####. ####123456.789The 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.79The number has  valuethree digits to the right of the decimal point, but the number  patternhas only two. On the  formatmethod of addressing this arrest.
123.78000000.000000123.780It  patternspecifies the principal behind zeros, because the 0 character is used instead of the pound (#).
12345.67$ ####, ####. ####12,345.67 USDThe first letter in  patternthe dollar sign ($). Note that it immediately precedes the leftmost number in the  output.
12345.67\u00A5 ####, ####. ####South Korean WonThe  patternJapanese yen (¥) currency sign with 00A5 specifies the Unicode value.

Locale sensitive format ####

The previous example created  DecimalFormatan object for the default  Locale. If you want  DecimalFormata non-default object  Locale, you can instantiate it  NumberFormatand 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  applyLocalizedPatternmethod on the  DecimalFormatobject.

Edit formatting codes

You can use the DecimalFormatSymbols class   to change the symbols that appear in the formatted numbers that the formatmethod produces  . These symbols include the decimal separator, the aggregation separator, the minus sign, and the percent sign, among others.

The following example illustrates the  DecimalFormatSymbolsseparation by applying an odd format to a number. The unusual form is the result of calls to  setDecimalSeparator,  setGroupingSeparatorand  setGroupingSizemethods.

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:

iconsDescribe
X*0 or more cases of X
(X | Y)Either X or Y
X..YAny letter from X through Y, inclusive
S - TCharacters 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:

CodeDescribe
0number
#Number, zero appears as absent
.placeholder for decimal separator
And theplaceholder for separator grouping
eSeparates 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
XAny other characters can be used in the prefix or suffix
Used to quote special characters in a prefix or suffix
Play Now

LEAVE A REPLY

Please enter your comment!
Please enter your name here