18.5 Formatting and Parsing Number, Currency, and Percentage Values

The abstract class java.text.NumberFormat and its subclasses java.text.Decimal-Format and java.text.CompactNumberFormat provide methods for locale-sensitive formatting and parsing of number, currency, and percentage values.

Static Factory Methods to Create a Formatter

The abstract class NumberFormat provides static factory methods for creating locale-sensitive formatters for number, currency, and percentage values. However, the locale cannot be changed after the formatter is created. The factory methods return instances of the concrete classes DecimalFormat and CompactNumberFormat for formatting number, currency, and percentage values.

Click here to view code image

static NumberFormat getNumberInstance()
static NumberFormat getNumberInstance(Locale locale)

Return a general formatter for numbers—that is, a number formatter.

Click here to view code image

static NumberFormat getCurrencyInstance()
static NumberFormat getCurrencyInstance(Locale locale)

Return a formatter for currency values—that is, a currency formatter.

Click here to view code image

static NumberFormat getPercentInstance()
static NumberFormat getPercentInstance(Locale locale)

Return a formatter for percentages—that is, a percentage formatter.

Click here to view code image

static NumberFormat getCompactNumberInstance()
static NumberFormat getCompactNumberInstance(Locale locale,
                                             NumberFormat.Style formatStyle)

Return a compact number formatter with the default compact format style or a specific compact format style, respectively (p. 1120). For example, the value 2_345_678 can be formatted by a compact number formatter as “2M” with the SHORT compact form style and as “2 million” with the LONG compact form style, respectively, for the US locale.

In all cases, a locale can be specified to localize the formatter.

Formatting Number, Currency, and Percentage Values

A number formatter can be used to format a double, a long value, or an object. The abstract class NumberFormat provides the following concrete methods for this purpose. Depending on the number formatter, the formatting is locale-sensitive, determined by the default or a specific locale.

String format(double d)
String format(long l)

Formats the specified number and returns the resulting string.

Click here to view code image

String format(Object obj)              // inherited from the Format class.

Formats the specified object and returns the resulting string. For example, it can be used to format BigInteger and BigDecimal numbers.

The following code shows how we can create a number formatter for the Norwegian locale and one for the US locale, and use them to format numbers according to rules of the locale. The number formatted is a double (1a) or a BigDecimal (1b), giving the same results. Note that the grouping of the digits and the decimal separator used in formatting is according to the locale.

Click here to view code image

double num = 12345.6789;                                // (1a)
// BigDecimal num = new BigDecimal(“12345.6789”);       // (1b)
Locale locNOR = new Locale(“no”, “NO”);                 // Norway
NumberFormat nfNOR = NumberFormat.getNumberInstance(locNOR);
System.out.println(nfNOR.format(num));                  // 12 345,679
NumberFormat nfUS = NumberFormat.getNumberInstance(Locale.US);
System.out.println(nfUS.format(num));                   // 12,345.679

The following code shows how we can create a currency formatter for the Norwegian locale, and use it to format currency values according to this locale. Note the currency symbol and the grouping of the digits, with the amount being rounded to two decimal places. Also note that the delimiter between the currency symbol and the first digit is a non-breaking space (nbsp), having the unicode \u00a0; that is, it is not a normal space (\u0020). The grouping of digits is also with a nbsp. Such use of a nbsp is also locale-specific—formatting for the US locale has no nbsp, as can be seen below.

Click here to view code image

NumberFormat cfNOR = NumberFormat.getCurrencyInstance(locNOR);
String formattedCurrStr = cfNOR.format(num);
System.out.println(formattedCurrStr);              // kr 12 345,68 (with 2 nbsp)

NumberFormat cfUS = NumberFormat.getCurrencyInstance(Locale.US);
String formattedCurrStrUS = cfUS.format(num);
System.out.println(formattedCurrStrUS);            // $12,345.68

The value 1.0 is equivalent to 100%. A value is converted to a percentage by multiplying it by 100, and by default rounding it to an integer. By default, the result is rounded up only if the decimal part is equal to or greater than 0.5 after multiplying by 100. However, both the number of decimal places and the rounding behavior for the percentage value can be changed (p. 1122).

The following code shows how we can create a percentage formatter for the Norwegian locale, and use it to format percentage values according to this locale. Also note that the delimiter between the percentage number and the currency symbol is a non-breaking space. Again, such use of a nbsp is also locale-specific—formatting percentages for the US locale has no nbsp, as can be seen below. Note the rounding of the percentage values.

Click here to view code image double rebate = 0.746;
NumberFormat pfNOR = NumberFormat.getPercentInstance(locNOR);
String formattedPStr = pfNOR.format(rebate);
System.out.println(formattedPStr);                 // 75 %  (with nbsp)
NumberFormat pfUS = NumberFormat.getPercentInstance(Locale.US);
String formattedPStrUS = pfUS.format(rebate);
System.out.println(formattedPStrUS);               // 75%
System.out.println(pfUS.format(0.745));            // 74%

Leave a Reply

Your email address will not be published. Required fields are marked *