Number formatters convert binary numbers (integer or float data types) into text strings for meaningful display. The Taligent 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 ("1.0 Number"), and the localized format generated by the Taligent number formatters ("1.1 Number").
To See This...
Do This...
Localized number formats. In the international string, the separator characters, such as the decimal point, change to reflect the conventions used by different countries.
- Pull down the Locale menu
- Try several different locales with the up and down arrow keys (on Windows) or the mouse button (on Macintosh)
- The number in the 1.1 Number field changes to reflect the local conventions.
(The pattern will not change, unless Localized Pattern is checked.)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.
- Click on the Currency Format button
- Pull down the Locale menu and try several different locales
Setting formatting options, for example, specifying a precise number of decimal places.
- Type "3" in the Min field for Decimals --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.
- Select the Default Format button and the English (United States) locale
- Increase the number in the Max field for Decimals
- Delete the "#" characters at the end of the Pattern field and type in zeroes--the decimal places in the 1.1 Number field change to zeroes
You can edit the prefix and suffix strings, and create different patterns for positive and negative numbers.
- Type a plus sign ("+") in the Prefix field for positive numbers to modify the positive number pattern
- Delete the minus sign from the 1.0 Number field
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 1.0 Number field.
- Select the thousands separator character in the 1.1 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.
This page incorporates material or code copyrighted by Taligent, Inc. For more information on international resources, see their International Fact Sheet.