The JavaTM Tutorial

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

## Using Predefined Formats

> By invoking the methods provided by the `NumberFormat` class, you can format numbers, currencies, and percentages according to `Locale`. The material that follows demonstrates formatting techniques with a sample program called `NumberFormatDemo`.

### Numbers

> You can use the `NumberFormat` methods to format primitive-type numbers, such as `double`, and their corresponding wrapper objects, such as `Double`.

> The following code example formats a `Double` according to `Locale`. Invoking the `getNumberInstance` method returns a locale-specific instance of `NumberFormat`. The `format` method accepts the `Double` as an argument and returns the formatted number in a `String`.

> >     Double amount = new Double(345987.246);
> >     NumberFormat numberFormatter;
> >     String amountOut;

> >     numberFormatter = NumberFormat.getNumberInstance(currentLocale);
> >     amountOut = numberFormatter.format(amount);
> >     System.out.println(amountOut + " " +
> >                        currentLocale.toString());

> The output from this example shows how the format of the same number varies with `Locale`:

> >     345 987,246	 fr_FR
> >     345.987,246	 de_DE
> >     345,987.246	 en_US

### Currencies

> If you're writing business applications, you'll probably need to format and to display currencies. You format currencies in the same manner as numbers, except that you call `getCurrencyInstance` to create a formatter. When you invoke the `format` method, it returns a `String` that includes the formatted number and the appropriate currency sign.

> This code example shows how to format currency in a locale-specific manner:

> >     Double currency = new Double(9876543.21);
> >     NumberFormat currencyFormatter;
> >     String currencyOut;

> >     currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
> >     currencyOut = currencyFormatter.format(currency);
> >     System.out.println(currencyOut + " " +
> >                        currentLocale.toString());

> The output generated by the preceding lines of code is as follows:

> >     9 876 543,21 F	 fr_FR
> >     9.876.543,21 DM	 de_DE
> >     $9,876,543.21	 en_US

> At first glance this output may look wrong to you, because the numeric values are all the same. Of course, 9 876 543,21 F is not equivalent to 9.876.543,21 DM. However, bear in mind that the `NumberFormat` class is unaware of exchange rates. The methods belonging to the `NumberFormat` class format currencies but do not convert them.

### Percentages

> You can also use the methods of the `NumberFormat` class to format percentages. To get the locale-specific formatter, invoke the `getPercentInstance` method. With this formatter, a decimal fraction such as 0.75 is displayed as 75%.

> The following code sample shows how to format a percentage.

> >     Double percent = new Double(0.75);
> >     NumberFormat percentFormatter;
> >     String percentOut;

> >     percentFormatter = NumberFormat.getPercentInstance(currentLocale);
> >     percentOut = percentFormatter.format(percent);

| Start of Tutorial > Start of Trail > Start of Lesson | Search Feedback Form

Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.