localeconv() — Retrieve Information from the Environment

Format

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

Language Level: ANSI

Threadsafe: Yes.

Locale Sensitive: The behavior of this function might be affected by the LC_NUMERIC and LC_MONETARY categories of the current locale. For more information, see Understanding CCSIDs and Locales.

Description

The localeconv() sets the components of a structure having type struct lconv to values appropriate for the current locale. The structure might be overwritten by another call to localeconv(), or by calling the setlocale() function.

The structure contains the following elements (defaults shown are for the C locale):

Element Purpose of Element Default
char *decimal_point Decimal-point character used to format non-monetary quantities. "."
char *thousands_sep Character used to separate groups of digits to the left of the decimal-point character in formatted non-monetary quantities. ""
char *grouping String indicating the size of each group of digits in formatted non-monetary quantities. Each character in the string specifies the number of digits in a group. The initial character represents the size of the group immediately to the left of the decimal delimiter. The characters following this define succeeding groups to the left of the previous group. If the last character is not UCHAR_MAX, the grouping is repeated using the last character as the size. If the last character is UCHAR_MAX, grouping is only performed for the groups already in the string (no repetition). See Table 1 for an example of how grouping works. ""
char *int_curr_symbol International currency symbol for the current locale. The first three characters contain the alphabetic international currency symbol. The fourth character (usually a space) is the character used to separate the international currency symbol from the monetary quantity. ""
char *currency_symbol Local currency symbol of the current locale. ""
char *mon_decimal_point Decimal-point character used to format monetary quantities. ""
char *mon_thousands_sep Separator for digits in formatted monetary quantities. ""
char *mon_grouping String indicating the size of each group of digits in formatted monetary quantities. Each character in the string specifies the number of digits in a group. The initial character represents the size of the group immediately to the left of the decimal delimiter. The following characters define succeeding groups to the left of the previous group. If the last character is not UCHAR_MAX, the grouping is repeated using the last character as the size. If the last character is UCHAR_MAX, grouping is only performed for the groups already in the string (no repetition). See Table 1 for an example of how grouping works. ""
char *positive_sign String indicating the positive sign used in monetary quantities. ""
char *negative_sign String indicating the negative sign used in monetary quantities. ""
char int_frac_digits The number of displayed digits to the right of the decimal place for internationally formatted monetary quantities. UCHAR_MAX
char frac_digits Number of digits to the right of the decimal place in monetary quantities. UCHAR_MAX
char p_cs_precedes 1 if the currency_symbol precedes the value for a nonnegative formatted monetary quantity; 0 if it does not. UCHAR_MAX
char p_sep_by_space 1 if the currency_symbol is separated by a space from the value of a nonnegative formatted monetary quantity; 0 if it does not. UCHAR_MAX
char n_cs_precedes 1 if the currency_symbol precedes the value for a negative formatted monetary quantity; 0 if it does not. UCHAR_MAX
char n_sep_by_space 1 if the currency_symbol is separated by a space from the value of a negative formatted monetary quantity; 0 if it does not. UCHAR_MAX
char p_sign_posn Value indicating the position of the positive_sign for a nonnegative formatted monetary quantity. UCHAR_MAX
char n_sign_posn Value indicating the position of the negative_sign for a negative formatted monetary quantity. UCHAR_MAX

Pointers to strings with a value of "" indicate that the value is not available in the C locale or is of zero length. Elements with char types with a value of UCHAR_MAX indicate that the value is not available in the current locale.

The n_sign_posn and p_sign_posn elements can have the following values:

Value
Meaning
0
The quantity and currency_symbol are enclosed in parentheses.
1
The sign precedes the quantity and currency_symbol.
2
The sign follows the quantity and currency_symbol.
3
The sign precedes the currency_symbol.
4
The sign follows the currency_symbol.

Grouping Example

Table 1. Grouping Example
Locale Source Grouping String Number Formatted Number
-1 0x00 123456789 123456789
3 0x0300 123456789 123,456,789
3;-1 0x03FF00 123456789 123456,789
3;2;1 0x03020100 123456789 1,2,3,4,56,789

Monetary Formatting Example:

Table 2. Monetary Formatting Example
Country Positive Format Negative Format International Format
Italy L.1.230 -L.1.230 ITL.1.230
Netherlands F 1.234,56 F -1.234,56 NLG 1.234,56
Norway kr1.234,56 kr1.234,56- NOK1.234,56
Switzerland SFRs.1,234.56 SFrx.1,234.56C CHF 1,234.56

The above table was generated by locales with the following monetary fields:

Table 3. Monetary Fields
Italy Netherlands Norway Switzerland
int_curr_symbol "ITL." "NLG" "NOK" "CHF"
currency_symbol "L." "F" "kr" "SFrs."
mon_decimal_point "" "," "," "."
mon_thousands_sep "." "." "." ","
mon_grouping "\3" "\3" "\3" "\3"
positive_sign "" "" "" ""
negative_sign "-" "-" "-" "C"
int_frac_digits 0 2 2 2
frac_digits 0 2 2 2
p_cs_precedes 1 1 1 1
p_sep_by_space 0 1 0 0
n_cs_preceds 1 1 1 1
n_sep_by_space 0 1 0 0
p_sep_posn 1 1 1 1
n_sign_posn 1 4 2 2

Return Value

The localeconv() function returns a pointer to the structure.

Example that uses *CLD locale objects

This example prints out the default decimal point for your locale and then the decimal point for the LC_C_FRANCE locale.

#include <stdio.h>
#include <locale.h>
 
int main(void) {
 
   char * string;
   struct lconv * mylocale;
   mylocale = localeconv();
 
   /* Display default decimal point */
 
   printf("Default decimal point is a %s\n", mylocale->decimal_point);
 
   if (NULL != (string = setlocale(LC_ALL, LC_C_FRANCE))) {
       mylocale = localeconv();
 
      /* A comma is set to be the decimal point when the locale is LC_C_FRANCE*/
 
      printf("France's decimal point is a %s\n", mylocale->decimal_point);
 
   } else {
 
      printf("setlocale(LC_ALL, LC_C_FRANCE) returned <NULL>\n");
   }
   return 0;
}

Example that uses *LOCALE objects

 /************************************************************************
 This example prints out the default decimal point for
 the C locale and then the decimal point for the French
 locale using a *LOCALE object called
 "QSYS.LIB/MYLIB.LIB/LC_FRANCE.LOCALE".
 
  Step 1: Create a French *LOCALE object by entering the command
    CRTLOCALE LOCALE('QSYS.LIB/MYLIB.LIB/LC_FRANCE.LOCALE') +
              SRCFILE('QSYS.LIB/QSYSLOCALE.LIB/QLOCALESRC.FILE/ +
                       FR_FR.MBR') CCSID(297)                            *
  Step 2: Compile the following C source, specifying
          LOCALETYPE(*LOCALE) on the compilation command.
  Step 3: Run the program.
 ************************************************************************/
 
 #include <stdio.h>
 #include <locale.h>
 int main(void) {
   char * string;
   struct lconv * mylocale;
   mylocale = localeconv();
 
   /* Display default decimal point */
   printf("Default decimal point is a %s\n", mylocale->decimal_point);
   if (NULL != (string = setlocale(LC_ALL,
       "QSYS.LIB/MYLIB.LIB/LC_FRANCE.LOCALE"))) {
      mylocale = localeconv();
 
      /* A comma is set to be the decimal point in the French locale */
      printf("France's decimal point is a %s\n", mylocale->decimal_point);
   } else {
      printf("setlocale(LC_ALL, \"QSYS.LIB/MYLIB.LIB/LC_FRANCE.LOCALE\") \
              returned <NULL>\n");
   }
   return 0;
 }

Related Information



[ Top of Page | Previous Page | Next Page | Contents | Index ]