O'Reilly logo

PURE Java™ 2 by Kenneth Litwak

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Formatting Numeric Data

This section describes how to format numbers and currency. When formatting numeric data, you can use two approaches:

  • Use the predefined formats available through the NumberFormat class.

  • Use a pattern for formatting data, such as a String pattern with DecimalFormat. This allows you to format values in a locale-independent way, such as replacing commas with decimal points. You use a pattern String to accomplish this.

Numeric Formatting: Code Example

There are two main ways to customize numeric values: NumberFormat and DecimalFormat. The former is for simple, straightforward formatting, and the latter is for custom formatting. Listing 18.4 shows how to use NumberFormat for predefined formats and DecimalFormat for customized ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required