Number formatters convert binary numbers (integer or float data types) into text strings for meaningful display. The number formatters:
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 formatsthe 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. |
|
||||
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. |
|
||||
Setting formatting options, for example, specifying a precise number of decimal places. |
|
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. |
|
||||||
You can edit the prefix and suffix strings, and create different patterns for positive and negative numbers. |
|
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. |
|
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!