## Number Formatting Demo Guide

(Java not enabled in this browser)

Number formatters convert binary numbers (integer or float data types) into text strings for meaningful display. The number formatters:

- Provide control over every aspect of the display, including separator characters, optional digits, maximum and minimum decimal and integer places, and optional prefixes and suffixes
- Can produce currency and percentage formats and can be extended to produce other formats
- Allow you to specify new formats with string patterns and can retrieve the normalized pattern
- Can parse anything they can format, for use when converting text input into binary data

Localized Formats Creating Formats with Patterns Parsing Text

### Localized Formats

The number formatters let you format numbers in the ways that are appropriate for any user's conventions, including country-specific formats such as currencies. You can also specify other formatting options such as minimum and maximum number of digits displayed.

The applet displays the number in two formats—the Java 1.0 representation ("Java 1.0 Number"), and the localized format generated by the number formatters ("New Number").

To See This... | Do This...
--- | ---

Localized number formats. In the international string, the separator characters change to reflect the conventions used by different countries.

1. | Pull down the Locale menu
2. | Try several different locales with the up and down arrow keys (on Windows) or the mouse button (on Macintosh)

Localized currency formats. The formatter adds a prefix or suffix to indicate the currency unit. It also uses a minus sign or parentheses to indicate negative numbers, as appropriate.

1. | Click on the Currency button
2. | Pull down the Locale menu and try several different locales

Setting formatting options, for example, specifying a precise number of decimal places.

1. | Type "3" in the Min field for Decimal Count —the formatter appends zeroes to display the specified number of decimal places

### Creating Formats with Patterns

A string pattern can control the output format for a number formatter, making it easy for you to edit a format or provide an editing mechanism for end users. The pattern also changes to reflect any changes you make programmatically.

Patterns can either be invariant (for use only by programmers) or localized (for use by end users). Click on the Localized Pattern box to change the state.

To See This... | Do This...
--- | ---

You can edit the pattern to create a new format.

1. | Select the Default Format button and the English (United States) locale
2. | Increase the maximum number for Decimal Count
3. | Delete the "#" characters at the end of the Pattern field and type in zeroes—the decimal places in the New Number field change to zeroes

You can edit the prefix and suffix strings, and create different patterns for positive and negative numbers.

1. | Type a plus sign ("+") in the Prefix field for positive numbers to modify the positive number pattern
2. | Delete the minus sign from the Java 1.0 number

### Parsing Text

The number formatters not only format for any language, but can parse numbers displayed in any language back into binary format.

To See This... | Do This...
--- | ---

Changes to the international number are parsed and reflected in the Java 1.0 Number field.

1. | Select the thousands separator character in the New Number field (the comma if you are still in the U.S. locale) and replace it with a number

The parsing mechanism is somewhat lenient. For example, a thousands separator character can be misplaced and the parser still returns the correct value.

Though not yet demonstrated here, the number formatters also handle IEEE number symbols such as the infinity sign and NaN (for example, 0/0), creating the correct results for both formatting and parsing. The formatters also return the alignment point so you can line up the end of integer portions correctly even without monospace fonts or with strange number conventions.

You can type in other numbers in either number field or enter a completely new pattern to see different formatting behaviors. Try it out!

The source.

© Copyright 1997. All rights reserved. Taligent, Inc., IBM Corp.