C library function - localeconv()
Description
localeconv() is a function in the C standard library a function in the C standard library used to obtain numeric and monetary formatting information about the current locale. It returns a pointer to struct lconv a structure that contains the localization formatting information for the current locale.
Declaration
Here is the declaration of the localeconv() function.
#include
struct lconv *localeconv(void);
Parameters
- NA
Return Value
- Returns a pointer to
struct lconva structure containing the localization information for the current locale.
The structure of the pointer to the current locale struct lconv is as follows:
struct lconv {
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; // 负值符号位置
};
Example
The following is example code that uses localeconv to obtain and print the numeric and monetary formatting information of the current locale:
Example
#include
#include
int
main
(
)
{
// Set the localization information to the default settings from the user environment variables
setlocale
(
LC_ALL
,
""
)
;
// Get the localization information of the current locale
struct
lconv
lc
=
localeconv
(
)
;
// Print the numeric formatting information
printf
(
"Decimal point character: %s\n"
,
lc
->
decimal_point
)
;
printf
(
"Thousands separator: %s\n"
,
lc
->
thousands_sep
)
;
printf
(
"Grouping: %s\n"
,
lc
->
grouping
)
;
// Print the monetary formatting information
printf
(
"International currency symbol: %s\n"
,
lc
->
int_curr_symbol
)
;
printf
(
"Local currency symbol: %s\n"
,
lc
->
currency_symbol
)
;
printf
(
"Monetary decimal point character: %s\n"
,
lc
->
mon_decimal_point
)
;
printf
(
"Monetary thousands separator: %s\n"
,
lc
->
mon_thousands_sep
)
;
printf
(
"Monetary grouping: %s\n"
,
lc
->
mon_grouping
)
;
printf
(
"Positive sign: %s\n"
,
lc
->
positive_sign
)
;
printf
(
"Negative sign: %s\n"
,
lc
->
negative_sign
)
;
printf
(
"International fractional digits: %d\n"
,
lc
->
int_frac_digits
)
;
printf
(
"Local fractional digits: %d\n"
,
lc
->
frac_digits
)
;
printf
(
"Positive currency symbol precedes: %d\n"
,
lc
->
p_cs_precedes
)
;
printf
(
"Positive currency symbol separated by space: %d\n"
,
lc
->
p_sep_by_space
)
;
printf
(
"Negative currency symbol precedes: %d\n"
,
lc
->
n_cs_precedes
)
;
printf
(
"Negative currency symbol separated by space: %d\n"
,
lc
->
n_sep_by_space
)
;
printf
(
"Positive sign position: %d\n"
,
lc
->
p_sign_posn
)
;
printf
(
"Negative sign position: %d\n"
,
lc
->
n_sign_posn
)
;
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Decimal point character: .
Thousands separator: ,
Grouping:
International currency symbol: CNY
Local currency symbol: ¥
Monetary decimal point character: .
Monetary thousands separator: ,
Monetary grouping:
Positive sign:
Negative sign: -
International fractional digits: 2
Local fractional digits: 2
Positive currency symbol precedes: 1
Positive currency symbol separated by space: 0
Negative currency symbol precedes: 1
Negative currency symbol separated by space: 0
Positive sign position: 1
Negative sign position: 4
Code Analysis
-
Numeric formatting information:
decimal_point: the decimal-point character, e.g. ".".thousands_sep: the thousands-separator character, e.g. ",".grouping: the digit grouping method.
-
Monetary formatting information:
int_curr_symbol: the international currency symbol, e.g. "USD".currency_symbol: the local currency symbol, e.g. "$".mon_decimal_point: the monetary decimal-point character.mon_thousands_sep: the monetary thousands-separator character.mon_grouping: the monetary digit grouping method.positive_sign: the positive sign, e.g. "".negative_sign: the negative sign, e.g. "-".int_frac_digits: the number of international fractional digits.frac_digits: the number of local fractional digits.p_cs_precedes: whether the currency symbol precedes positive values (1 for preceding, 0 for following).p_sep_by_space: whether there is a space between the currency symbol and the value for positive amounts (1 for a space, 0 for no space).n_cs_precedes: whether the currency symbol precedes negative values (1 for preceding, 0 for following).n_sep_by_space: whether there is a space between the currency symbol and the value for negative amounts (1 for a space, 0 for no space).p_sign_posn: the position of the positive sign (1 for preceding, 2 for following, 3 for adjacent to the value, 4 for adjacent to the currency symbol).n_sign_posn: the position of the negative sign (1 for preceding, 2 for following, 3 for adjacent to the value, 4 for adjacent to the currency symbol).
Summary
localeconv The function allows a program to obtain the numeric and monetary formatting information of the current locale. This is very useful for writing internationalized programs that need to handle multiple languages and currency formats. By using the setlocale and localeconv function, programmers can localize their programs to meet the needs of different user groups.
C Standard Library -