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 ...

Get PURE Java™ 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.