Logo Search packages:      
Sourcecode: icu version File versions

Typedefs | Enumerations | Functions

unum.h File Reference

C API: NumberFormat. More...

#include "unicode/utypes.h"
#include "unicode/localpointer.h"
#include "unicode/uloc.h"
#include "unicode/umisc.h"
#include "unicode/parseerr.h"
Include dependency graph for unum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef enum UCurrencySpacing UCurrencySpacing
typedef void * UNumberFormat
typedef enum UNumberFormatAttribute UNumberFormatAttribute
typedef enum
UNumberFormatPadPosition 
UNumberFormatPadPosition
typedef enum
UNumberFormatRoundingMode 
UNumberFormatRoundingMode
typedef enum UNumberFormatStyle UNumberFormatStyle
typedef enum UNumberFormatSymbol UNumberFormatSymbol
typedef enum
UNumberFormatTextAttribute 
UNumberFormatTextAttribute

Enumerations

enum  UCurrencySpacing { UNUM_CURRENCY_MATCH, UNUM_CURRENCY_SURROUNDING_MATCH, UNUM_CURRENCY_INSERT, UNUM_CURRENCY_SPACING_COUNT }
enum  UNumberFormatAttribute {
  UNUM_PARSE_INT_ONLY, UNUM_GROUPING_USED, UNUM_DECIMAL_ALWAYS_SHOWN, UNUM_MAX_INTEGER_DIGITS,
  UNUM_MIN_INTEGER_DIGITS, UNUM_INTEGER_DIGITS, UNUM_MAX_FRACTION_DIGITS, UNUM_MIN_FRACTION_DIGITS,
  UNUM_FRACTION_DIGITS, UNUM_MULTIPLIER, UNUM_GROUPING_SIZE, UNUM_ROUNDING_MODE,
  UNUM_ROUNDING_INCREMENT, UNUM_FORMAT_WIDTH, UNUM_PADDING_POSITION, UNUM_SECONDARY_GROUPING_SIZE,
  UNUM_SIGNIFICANT_DIGITS_USED, UNUM_MIN_SIGNIFICANT_DIGITS, UNUM_MAX_SIGNIFICANT_DIGITS, UNUM_LENIENT_PARSE
}
enum  UNumberFormatPadPosition { UNUM_PAD_BEFORE_PREFIX, UNUM_PAD_AFTER_PREFIX, UNUM_PAD_BEFORE_SUFFIX, UNUM_PAD_AFTER_SUFFIX }
enum  UNumberFormatRoundingMode {
  UNUM_ROUND_CEILING, UNUM_ROUND_FLOOR, UNUM_ROUND_DOWN, UNUM_ROUND_UP,
  UNUM_FOUND_HALFEVEN, UNUM_ROUND_HALFDOWN, UNUM_ROUND_HALFUP, UNUM_ROUND_UNNECESSARY,
  UNUM_ROUND_HALFEVEN = UNUM_FOUND_HALFEVEN
}
enum  UNumberFormatStyle {
  UNUM_PATTERN_DECIMAL = 0, UNUM_DECIMAL = 1, UNUM_CURRENCY, UNUM_PERCENT,
  UNUM_SCIENTIFIC, UNUM_SPELLOUT, UNUM_ORDINAL, UNUM_DURATION,
  UNUM_NUMBERING_SYSTEM, UNUM_PATTERN_RULEBASED, UNUM_CURRENCY_ISO, UNUM_CURRENCY_PLURAL,
  UNUM_FORMAT_STYLE_COUNT, UNUM_DEFAULT = UNUM_DECIMAL, UNUM_IGNORE = UNUM_PATTERN_DECIMAL
}
enum  UNumberFormatSymbol {
  UNUM_DECIMAL_SEPARATOR_SYMBOL = 0, UNUM_GROUPING_SEPARATOR_SYMBOL = 1, UNUM_PATTERN_SEPARATOR_SYMBOL = 2, UNUM_PERCENT_SYMBOL = 3,
  UNUM_ZERO_DIGIT_SYMBOL = 4, UNUM_DIGIT_SYMBOL = 5, UNUM_MINUS_SIGN_SYMBOL = 6, UNUM_PLUS_SIGN_SYMBOL = 7,
  UNUM_CURRENCY_SYMBOL = 8, UNUM_INTL_CURRENCY_SYMBOL = 9, UNUM_MONETARY_SEPARATOR_SYMBOL = 10, UNUM_EXPONENTIAL_SYMBOL = 11,
  UNUM_PERMILL_SYMBOL = 12, UNUM_PAD_ESCAPE_SYMBOL = 13, UNUM_INFINITY_SYMBOL = 14, UNUM_NAN_SYMBOL = 15,
  UNUM_SIGNIFICANT_DIGIT_SYMBOL = 16, UNUM_MONETARY_GROUPING_SEPARATOR_SYMBOL = 17, UNUM_ONE_DIGIT_SYMBOL = 18, UNUM_TWO_DIGIT_SYMBOL = 19,
  UNUM_THREE_DIGIT_SYMBOL = 20, UNUM_FOUR_DIGIT_SYMBOL = 21, UNUM_FIVE_DIGIT_SYMBOL = 22, UNUM_SIX_DIGIT_SYMBOL = 23,
  UNUM_SEVEN_DIGIT_SYMBOL = 24, UNUM_EIGHT_DIGIT_SYMBOL = 25, UNUM_NINE_DIGIT_SYMBOL = 26, UNUM_FORMAT_SYMBOL_COUNT = 27
}
enum  UNumberFormatTextAttribute {
  UNUM_POSITIVE_PREFIX, UNUM_POSITIVE_SUFFIX, UNUM_NEGATIVE_PREFIX, UNUM_NEGATIVE_SUFFIX,
  UNUM_PADDING_CHARACTER, UNUM_CURRENCY_CODE, UNUM_DEFAULT_RULESET, UNUM_PUBLIC_RULESETS
}

Functions

U_STABLE void U_EXPORT2 unum_applyPattern (UNumberFormat *format, UBool localized, const UChar *pattern, int32_t patternLength, UParseError *parseError, UErrorCode *status)
U_STABLE UNumberFormat *U_EXPORT2 unum_clone (const UNumberFormat *fmt, UErrorCode *status)
U_STABLE void U_EXPORT2 unum_close (UNumberFormat *fmt)
U_STABLE int32_t U_EXPORT2 unum_countAvailable (void)
U_STABLE int32_t U_EXPORT2 unum_format (const UNumberFormat *fmt, int32_t number, UChar *result, int32_t resultLength, UFieldPosition *pos, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_formatDecimal (const UNumberFormat *fmt, const char *number, int32_t length, UChar *result, int32_t resultLength, UFieldPosition *pos, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_formatDouble (const UNumberFormat *fmt, double number, UChar *result, int32_t resultLength, UFieldPosition *pos, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_formatDoubleCurrency (const UNumberFormat *fmt, double number, UChar *currency, UChar *result, int32_t resultLength, UFieldPosition *pos, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_formatInt64 (const UNumberFormat *fmt, int64_t number, UChar *result, int32_t resultLength, UFieldPosition *pos, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_getAttribute (const UNumberFormat *fmt, UNumberFormatAttribute attr)
U_STABLE const char *U_EXPORT2 unum_getAvailable (int32_t localeIndex)
U_STABLE double U_EXPORT2 unum_getDoubleAttribute (const UNumberFormat *fmt, UNumberFormatAttribute attr)
U_STABLE const char *U_EXPORT2 unum_getLocaleByType (const UNumberFormat *fmt, ULocDataLocaleType type, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_getSymbol (const UNumberFormat *fmt, UNumberFormatSymbol symbol, UChar *buffer, int32_t size, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_getTextAttribute (const UNumberFormat *fmt, UNumberFormatTextAttribute tag, UChar *result, int32_t resultLength, UErrorCode *status)
U_STABLE UNumberFormat *U_EXPORT2 unum_open (UNumberFormatStyle style, const UChar *pattern, int32_t patternLength, const char *locale, UParseError *parseErr, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_parse (const UNumberFormat *fmt, const UChar *text, int32_t textLength, int32_t *parsePos, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_parseDecimal (const UNumberFormat *fmt, const UChar *text, int32_t textLength, int32_t *parsePos, char *outBuf, int32_t outBufLength, UErrorCode *status)
U_STABLE double U_EXPORT2 unum_parseDouble (const UNumberFormat *fmt, const UChar *text, int32_t textLength, int32_t *parsePos, UErrorCode *status)
U_STABLE double U_EXPORT2 unum_parseDoubleCurrency (const UNumberFormat *fmt, const UChar *text, int32_t textLength, int32_t *parsePos, UChar *currency, UErrorCode *status)
U_STABLE int64_t U_EXPORT2 unum_parseInt64 (const UNumberFormat *fmt, const UChar *text, int32_t textLength, int32_t *parsePos, UErrorCode *status)
U_STABLE void U_EXPORT2 unum_setAttribute (UNumberFormat *fmt, UNumberFormatAttribute attr, int32_t newValue)
U_STABLE void U_EXPORT2 unum_setDoubleAttribute (UNumberFormat *fmt, UNumberFormatAttribute attr, double newValue)
U_STABLE void U_EXPORT2 unum_setSymbol (UNumberFormat *fmt, UNumberFormatSymbol symbol, const UChar *value, int32_t length, UErrorCode *status)
U_STABLE void U_EXPORT2 unum_setTextAttribute (UNumberFormat *fmt, UNumberFormatTextAttribute tag, const UChar *newValue, int32_t newValueLength, UErrorCode *status)
U_STABLE int32_t U_EXPORT2 unum_toPattern (const UNumberFormat *fmt, UBool isPatternLocalized, UChar *result, int32_t resultLength, UErrorCode *status)

Detailed Description

C API: NumberFormat.

Number Format C API

Number Format C API Provides functions for formatting and parsing a number. Also provides methods for determining which locales have number formats, and what their names are.

UNumberFormat helps you to format and parse numbers for any locale. Your code can be completely independent of the locale conventions for decimal points, thousands-separators, or even the particular decimal digits used, or whether the number format is even decimal. There are different number format styles like decimal, currency, percent and spellout.

To format a number for the current Locale, use one of the static factory methods:

 
    UChar myString[20];
    double myNumber = 7.0;
    UErrorCode status = U_ZERO_ERROR;
    UNumberFormat* nf = unum_open(UNUM_DEFAULT, NULL, -1, NULL, NULL, &status);
    unum_formatDouble(nf, myNumber, myString, 20, NULL, &status);
    printf(" Example 1: %s\n", austrdup(myString) ); //austrdup( a function used to convert UChar* to char*)

If you are formatting multiple numbers, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.

 
 uint32_t i, resultlength, reslenneeded;
 UErrorCode status = U_ZERO_ERROR;
 UFieldPosition pos;
 uint32_t a[] = { 123, 3333, -1234567 };
 const uint32_t a_len = sizeof(a) / sizeof(a[0]);
 UNumberFormat* nf;
 UChar* result = NULL;

 nf = unum_open(UNUM_DEFAULT, NULL, -1, NULL, NULL, &status);
 for (i = 0; i < a_len; i++) {
    resultlength=0;
    reslenneeded=unum_format(nf, a[i], NULL, resultlength, &pos, &status);
    result = NULL;
    if(status==U_BUFFER_OVERFLOW_ERROR){
       status=U_ZERO_ERROR;
       resultlength=reslenneeded+1;
       result=(UChar*)malloc(sizeof(UChar) * resultlength);
       unum_format(nf, a[i], result, resultlength, &pos, &status);
    }
    printf( " Example 2: %s\n", austrdup(result));
    free(result);
 }

To format a number for a different Locale, specify it in the call to unum_open().

 
     UNumberFormat* nf = unum_open(UNUM_DEFAULT, NULL, -1, "fr_FR", NULL, &success)

You can use a NumberFormat API unum_parse() to parse.

 
    UErrorCode status = U_ZERO_ERROR;
    int32_t pos=0;
    int32_t num;
    num = unum_parse(nf, str, u_strlen(str), &pos, &status);

Use UNUM_DECIMAL to get the normal number format for that country. There are other static options available. Use UNUM_CURRENCY to get the currency number format for that country. Use UNUM_PERCENT to get a format for displaying percentages. With this format, a fraction from 0.53 is displayed as 53%.

Use a pattern to create either a DecimalFormat or a RuleBasedNumberFormat formatter. The pattern must conform to the syntax defined for those formatters.

You can also control the display of numbers with such function as unum_getAttribues() and unum_setAtributes(), which let you set the miminum fraction digits, grouping, etc.

See also:
UNumberFormatAttributes for more details

You can also use forms of the parse and format methods with ParsePosition and UFieldPosition to allow you to:

It is also possible to change or set the symbols used for a particular locale like the currency symbol, the grouping seperator , monetary seperator etc by making use of functions unum_setSymbols() and unum_getSymbols().

Definition in file unum.h.


Generated by  Doxygen 1.6.0   Back to index