Previous Page
Next Page

localeconv

Obtains the conventions of the current locale

#include <locale.h>
struct lconv *localeconv( void );

The localeconv( ) function returns a pointer to a structure containing complete information on the locale-specific conventions for formatting numeric and monetary information. The values returned reflect the conventions of the current locale, which you can query or set using the setlocale( ) function.

The structure that localeconv( ) fills in has the type struct lconv, which is defined in the header file locale.h. The members of this structure describe how to format monetary as well as non-monetary numeric values in the locale. In C99, moreover, two sets of information describing monetary formatting are present: one describing local usage and one describing international usage, which calls for standard alphabetic currency symbols, and may also use a different number of decimal places.

The full set of members and their order in the structure may vary from one implementation to another, but they must include at least the members described here:


char *decimal_point;

The decimal point character, except when referring to money. In the default locale C, this pointer refers to the value ".".


char *thousands_sep;

The digit-grouping character: for example, the comma in "65,536". In spite of the name, not all locales group digits by thousands; see the next member, grouping.


char *grouping;

This pointer refers not to a text string, but to an array of numeric char values with the following meaning: the first element in the array is the number of digits in the rightmost digit group. Each successive element is the number of digits in the next group to the left. The value CHAR_MAX means that the remaining digits are not grouped at all; the value 0 means that the last group size indicated is used for all remaining digits. For example, the char array {'\3','\0'} indicates that all digits are grouped in threes.


char *mon_decimal_point;

Decimal point character for monetary values.


char *mon_thousands_sep;

The digit-grouping character for monetary values.


char *mon_grouping;

Like the grouping element, but for monetary values.


char *positive_sign;

The sign used to indicate positive monetary values.


char *negative_sign;

The sign used to indicate negative monetary values.


char *currency_symbol;

The currency symbol in local use: in the United States, this would be "$", while the abbreviation used in international finance, "USD", would be indicated by another structure member, int_currency_symbol.


char frac_digits;

The number of digits after the decimal point in monetary values, in local usage.


char p_cs_precedes;

The value 1 means the local currency_symbol is placed before positive numbers (as in U.S. dollars: "$10.99"); 0 means the symbol comes after the number (as in the Canadian French locale, "fr_CA": "10,99 $").


char n_cs_precedes;

The value 1 means the local currency_symbol is placed before negative numbers; 0 means the symbol comes after the number.


char p_sep_by_space;

The value 1 means a space is inserted between currency_symbol and a positive number.


char n_sep_by_space;

The value 1 means a space is inserted between currency_symbol and a negative number.


char p_sign_posn;

See next item.


char n_sign_posn;

These values indicate the positions of the positive and negative signs, as follows:


0

The number and currency_symbol are enclosed together in parentheses.


1

The sign string is placed before the number and currency_symbol.


2

The sign string is placed after the number and currency_symbol.


3

The sign string is placed immediately before the currency_symbol.


4

The sign string is placed immediately after the currency_symbol.


char *int_curr_symbol;

This pointer indicates a null-terminated string containing the three-letter international symbol for the local currency (as specified in ISO 4217), and a separator character in the fourth position.


char int_frac_digits;

The number of digits after the decimal point in monetary values, in international usage.


char int_p_cs_precedes; (C99)

The value 1 means that int_curr_symbol is placed before positive numbers; 0 means the symbol comes after the number.


char int_n_cs_precedes; (C99)

The value 1 means int_curr_symbol is placed before negative numbers; 0 means the symbol comes after the number.


char int_p_sep_by_space; (C99)

The value 1 means a space is inserted between int_curr_symbol and a positive number.


char int_n_sep_by_space; (C99)

The value 1 means a space is inserted between int_curr_symbol and a negative number.


char int_p_sign_posn; (C99)

See next item.


char int_n_sign_posn; (C99)

These values indicate the positions of the positive and negative signs with respect to int_curr_symbol in the same way as p_sign_posn and n_sign_posn indicate the sign positions with respect to currency_symbol.

In the default locale, C, all of the char members have the value CHAR_MAX, and all of the char * members point to an empty string (""), except decimal_point, which points to the string ".".

Example

long long cents;         // Amount in cents or customary fraction of
                         // currency unit.
struct lconv *locinfo;
wchar_t number[128] = { L'\0' },  prefix[32]  = { L'\0' },
        suffix[32]  = { L'\0' };

// Use system's current locale.
char *localename = setlocale( LC_MONETARY, "" );

locinfo = localeconv( );

/* ... */

if ( cents >= 0 ) // For positive amounts,
                  // use 'p_...' members of lconv structure.
{
  if ( locinfo->p_cs_precedes )    // If currency symbol before number ...
  {                                // ... prepare prefix ...
     mbstowcs( prefix, locinfo->currency_symbol, 32 );
      if ( locinfo->p_sep_by_space )
        wcscat( prefix, L" " );    // ... maybe with a space.
   }
/* ... else etc. ... */

See Also

setlocale( )


Previous Page
Next Page