Controlling basic TNumberFormatter behavior

TNumberFormatter allows you to specify:

Other formatting characteristics--for example, the character used to separate portions of the integer or the character used to separate the integer from the decimal--are set within the TNumberFormatter subclass that formats that type of number.

Classes derived from TNumberFormatter provide default functionality for all aspects of number formatting. You can use default number formatters in many situations simply by instantiating and calling the formatter. You can also use the number formatter specified by the current locale, using the identifier TLocale::kNumberFormatID. For example:

      TLocale currentLocale = TLocale::GetCurrentLocale();
      
      TLocaleItem<TNumberFormatter> item;
      TDeleterFor<TNumberFormatter> numberFormatter = 
              item.CopyItem( TLocale::kNumberFormatID, currentLocale );
      
      TStandardText numText;
      TNumberFormatResult result;
      
      numberFormatter->Format( TFormattableNumber(9999), numText, result );
      // numText now contains a formatted string representing the number 9999 in the
      // format specified by the current locale.
You can also change each number formatting characteristic to produce customized number formatters for different needs.

Formatting positive and negative numbers

Each TNumberFormatter instance can produce distinct formatting for positive and negative numbers by appending text to the beginning or end of a formatted number. Some examples are:

By default, TNumberFormatter assumes that text is provided to append before and after every number. You can, however, provide empty text strings for some or all of the affixes.

To set the affixes used to indicate the positive or negative signs, use the SetPlus or SetMinus member functions. These functions take text instances containing the affix strings. For example, to make the number formatter myNumberFormatter produce negative numbers formatted like (9,999), you might use the following code:

      UniChar px = TGeneralPunctuation::kLeftParenthesis;
      TStandardText prefix(&px, 1);
      UniChar sx = TGeneralPunctuation::kRightParenthesis;
      TStandardText suffix(&sx, 1);
      
      myNumberFormatter->SetMinus( prefix, suffix );
TNumberFormatter always appends sign affixes to negative numbers. You can, however, choose not to append any affixes, even empty ones, to formatted positive numbers. You control this behavior with the TNumberFormatter::SetShowPlusSign member function.

TNumberFormatter provides the following default behavior for sign indication:

Sign Prefix Suffix Example
Positive Empty string Empty string 9,999
Negative TMathematicalOperators::kMinusSign Empty string -9,999

Setting text identifiers

You can specify the text symbols to indicate infinity or NaN--the number formatter stores the text strings and uses them when both formatting and scanning.

To set a string to indicate infinity, use the TNumberFormatter::SetInfinitySign function. By default, number formatters use the Unicode character TMathematicalOperators::kInfinity ().

To set a string to indicate a NaN, use the TNumberFormatter::SetNanSign function. By default, number formatters use the Unicode character TGeneralPunctuation::kQuestionMark.

Setting the formatting range

Each TNumberFormatter instance has an associated numerical range, within which it can produce valid results. The TNumberFormatter default for this range is negative infinity to positive infinity, but you might want the number formatters for some numbering systems to manipulate only a certain range of numbers. For example, the Roman numbering system is generally used only to represent numbers between 1 and 5,000.

To change the valid range for a number formatter, use the SetMinNumber and SetMaxNumber member functions. These functions take a double as the argument.

Each TNumberFormatter instance also has an associated number formatter that it uses whenever you ask it to handle any number out of its range. The TNumberFormatter default for this formatter is a TUniversalNumberFormatter that uses the digits in the Roman script. This number formatter has a universal range; it can handle any number, including NaNs. This mechanism guarantees that you get a result whenever you format a number, although it does not guarantee the level of accuracy.

Use the TNumberFormatter::AdoptOutOfBoundsNumberFormatter member function to change the default out-of-bounds number formatter--for example, to use a formatter other than TUniversalNumberFormatter or to create a localized instance of TUniversalNumberFormatter that uses digits other than those in the Roman script. This function checks the range of the formatter before adopting it to ensure that it has a larger range than the original formatter.


Mapping characters to numeric values

Each TNumberFormatter instance uses a class derived from TNumerals that provides the correct set of individual character-value mappings--for example, mapping the character TASCII::kDigitOne to the value 1 or the character TRomanNumeral::kOne to the value 1. During conversion operations, TNumberFormatter calls either TNumerals::NumeralToValue or TNumerals::ValueToNumeral to get the correct mapping.

The system currently provides two concrete subclasses of TNumerals--TUnicodeDecimalNumerals and THybridNumerals. Use TUnicodeDecimalNumerals for a mapping of the characters and associated digit values defined by Unicode, and use THybridNumerals for arbitrary character-value mappings.


Using TUnicodeDecimalNumerals

TUnicodeDecimalNumerals uses the numeric characteristics defined by Unicode. It recognizes the characters defined by Unicode as digits, and maps them to the Unicode-assigned values.

During text scanning, the TUnicodeDecimalNumerals::NumeralToValue function accepts any character defined by Unicode as a digit and converts it to the corresponding value. Character-value mappings can be many-to-one. For example, the TUnicodeDigit characters kArabicIndicOne, kMalayalamOne, and kDevanagariOne all map to the value 1.

During formatting, the TUnicodeDecimalNumerals::ValueToNumeral function formats a numeric value into a character based on the mappings for a particular script. For example, when the function receives the value 1, it needs to know what script to use so that it knows whether kArabicIndicOne, kDevanagariOne, or some other character is appropriate.

You can specify a script when you instantiate TUnicodeDecimalNumerals, or later with the TUnicodeDecimalNumerals::SetScript member function. Specify the script with a TUnicode::EUnicodeScript value. The default value is EUnicodeScript::kRoman.


TPositionalNumberFormatter, TFloatingPointNumberFormatter, and TRationalNumberFormatter use this type of TNumerals by default.

Using THybridNumerals

Each THybridNumerals instance contains two sets of numeral-value pairs: one set used only for scanning and the other set used only for formatting.

The set of scanning pairs can represent many-to-one relationships; multiple scanning pairs can map different Unicode characters to the same value. For example, in the Roman numeral system, the characters i and I can both map to the value 1. You add a pair to the set of scanning pairs with the THybridNumerals::AddScanningPair member function.

The set of formatting pairs defines one-to-one relationships, and each numerical value can be mapped to only one Unicode character. For example, in the Roman numeral system, you have to choose whether to convert the value 1 into the character i or I. You add a pair to the set of formatting pairs with the THybridNumerals::AddFormattingPair member function.

NOTE When you add a formatting pair, the AddFormattingPair function also adds the pair to the set of scanning pairs.

A THybridNumerals object owns the scanning and formatting pairs, as shown in this figure:


You can also retrieve formatting and scanning pairs with the THybridNumerals functions GetFormattingPair and GetScanningPair:

      void GetFormattingPair ( unsigned short index, UniChar& numeral, long& value ) const;
      void GetScanningPair( unsigned short index, UniChar& numeral, long& value ) const;

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker