|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
You can use theDecimalFormatclass to format decimal numbers into locale-specific 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 formatting symbols, such as the decimal separator, you can use theDecimalFormatSymbolsin conjunction with theDecimalFormatclass. These classes offer a great deal of flexibility in the formatting of numbers, but they can make your code more complex.The text that follows uses examples that demonstrate the
DecimalFormatandDecimalFormatSymbolsclasses. The code examples in this material are from a sample program calledDecimalFormatDemo.
You specify the formatting properties ofDecimalFormatwith a patternString. The pattern determines what the formatted number looks like. For a full description of the pattern syntax, seeNumber Format Pattern Syntax.The example that follows creates a formatter by passing a pattern
Stringto theDecimalFormatconstructor. Theformatmethod accepts adoublevalue as an argument and returns the formatted number in aString:The output for the preceding lines of code is described in the following table. TheDecimalFormat myFormatter = new DecimalFormat(pattern); String output = myFormatter.format(value); System.out.println(value + " " + pattern + " " + output);valueis the number, adouble, that is to be formatted. Thepatternis theStringthat specifies the formatting properties. Theoutput, which is aString, represents the formatted number.
Output from
DecimalFormatDemoProgramvaluepatternoutputExplanation 123456.789 ###,###.### 123,456.789 The pound sign (#) denotes a digit, the comma is a placeholder for the grouping separator, and the period is a placeholder for the decimal separator. 123456.789 ###.## 123456.79 The valuehas three digits to the right of the decimal point, but thepatternhas only two. Theformatmethod handles this by rounding up.123.78 000000.000 000123.780 The patternspecifies leading and trailing zeros, because the 0 character is used instead of the pound sign (#).12345.67 $###,###.### $12,345.67 The first character in the patternis the dollar sign ($). Note that it immediately precedes the leftmost digit in the formattedoutput.12345.67 \u00A5###,###.### ¥12,345.67 The patternspecifies the currency sign for Japanese yen (¥) with the Unicode value 00A5.
The preceding example created aDecimalFormatobject for the defaultLocale. If you want aDecimalFormatobject for a nondefaultLocale, you instantiate aNumberFormatand then cast it toDecimalFormat. Here's an example:NumberFormat nf = NumberFormat.getNumberInstance(loc); DecimalFormat df = (DecimalFormat)nf; df.applyPattern(pattern); String output = df.format(value); System.out.println(pattern + " " + output + " " + loc.toString());Running the previous code example results in the output that follows. The formatted number, which is in the second column, varies with
Locale:###,###.### 123,456.789 en_US ###,###.### 123.456,789 de_DE ###,###.### 123 456,789 fr_FRSo far the formatting patterns discussed here follow the conventions of U.S. English. For example, in the pattern ###,###.## the comma is the thousands-separator and the period represents the decimal point. This convention is fine, provided that your end users aren't exposed to it. However, some applications, such as spreadsheets and report generators, allow the end users to define their own formatting patterns. For these applications the formatting patterns specified by the end users should use localized notation. In these cases you'll want to invoke the
applyLocalizedPatternmethod on theDecimalFormatobject.
You can use the DecimalFormatSymbolsclass to change the symbols that appear in the formatted numbers produced by the
formatmethod. These symbols include the decimal separator, the grouping separator, the minus sign, and the percent sign, among others.The next example demonstrates the
DecimalFormatSymbolsclass by applying a strange format to a number. The unusual format is the result of the calls to thesetDecimalSeparator,setGroupingSeparator, andsetGroupingSizemethods.DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale); unusualSymbols.setDecimalSeparator('|'); unusualSymbols.setGroupingSeparator('^'); String strange = "#,##0.###"; DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols); weirdFormatter.setGroupingSize(4); String bizarre = weirdFormatter.format(12345.678); System.out.println(bizarre);When run, this example prints the number in a bizarre format:
1^2345|678
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.