Latest web development tutorials

C standard library - <locale.h>

Brief introduction

locale.h header file defines specific geographical settings, such as date format and currency symbols.Next, we will introduce some macros, as well as an important structurestruct lconv and two important functions.

Macro library

Listed below are defined in the header file locale.h macros will be used in the following two functions:

序号宏 & 描述
1 LC_ALL
设置下面的所有选项。
2 LC_COLLATE
影响 strcoll 和 strxfrm 函数。
3 LC_CTYPE
影响所有字符函数。
4 LC_MONETARY
影响 localeconv 函数提供的货币信息。
5 LC_NUMERIC
影响 localeconv 函数提供的小数点格式化和信息。
6 LC_TIME
影响 strftime 函数。

Library Functions

The following lists the functions defined in the header file locale.h:

序号函数 & 描述
1 char *setlocale(int category, const char *locale)
设置或读取地域化信息。
2 struct lconv *localeconv(void)
设置或读取地域化信息。

Library structure

typedef struct {
   char *decimal_point;
   char *thousands_sep;
   char *grouping;	
   char *int_curr_symbol;
   char *currency_symbol;
   char *mon_decimal_point;
   char *mon_thousands_sep;
   char *mon_grouping;
   char *positive_sign;
   char *negative_sign;
   char int_frac_digits;
   char frac_digits;
   char p_cs_precedes;
   char p_sep_by_space;
   char n_cs_precedes;
   char n_sep_by_space;
   char p_sign_posn;
   char n_sign_posn;
} lconv

The following is a description of each field:

序号字段 & 描述
1 decimal_point
用于非货币值的小数点字符。
2 thousands_sep
用于非货币值的千位分隔符。
3 grouping
一个表示非货币量中每组数字大小的字符串。每个字符代表一个整数值,每个整数指定当前组的位数。值为 0 意味着前一个值将应用于剩余的分组。
4 int_curr_symbol
国际货币符号使用的字符串。前三个字符是由 ISO 4217:1987 指定的,第四个字符用于分隔货币符号和货币量。
5 currency_symbol
用于货币的本地符号。
6 mon_decimal_point
用于货币值的小数点字符。
7 mon_thousands_sep
用于货币值的千位分隔符。
8 mon_grouping
一个表示货币值中每组数字大小的字符串。每个字符代表一个整数值,每个整数指定当前组的位数。值为 0 意味着前一个值将应用于剩余的分组。
9 positive_sign
用于正货币值的字符。
10 negative_sign
用于负货币值的字符。
11 int_frac_digits
国际货币值中小数点后要显示的位数。
12 frac_digits
货币值中小数点后要显示的位数。
13 p_cs_precedes
如果等于 1,则 currency_symbol 出现在正货币值之前。如果等于 0,则 currency_symbol 出现在正货币值之后。
14 p_sep_by_space
如果等于 1,则 currency_symbol 和正货币值之间使用空格分隔。如果等于 0,则 currency_symbol 和正货币值之间不使用空格分隔。
15 n_cs_precedes
如果等于 1,则 currency_symbol 出现在负货币值之前。如果等于 0,则 currency_symbol 出现在负货币值之后。
16 n_sep_by_space
如果等于 1,则 currency_symbol 和负货币值之间使用空格分隔。如果等于 0,则 currency_symbol 和负货币值之间不使用空格分隔。
17 p_sign_posn
表示正货币值中正号的位置。
18 n_sign_posn
表示负货币值中负号的位置。

The following values forp_sign_posn and n_sign_posn:

描述
0 封装值和 currency_symbol 的括号。
1 放置在值和 currency_symbol 之前的符号。
2 放置在值和 currency_symbol 之后的符号。
3 紧挨着放置在值和 currency_symbol 之前的符号。
4 紧挨着放置在值和 currency_symbol 之后的符号。