C library functions -localeconv()
C Standard Library - <locale.h>
Description
localeconv()is the C standard library<locale.h>One of the functions, used to obtain numeric and currency formatting information for the current locale. It returns a pointer tostruct lconvPointer to a structure that contains localized formatting information for the current locale.
Declaration
The following is the declaration of the localeconv() function.
#include <locale.h> struct lconv *localeconv(void);
Parameter
- NA
Return Value
- Returns a pointer to
struct lconvPointer to a structure that contains localized information for the current locale.
current localestruct lconvThe structure of the pointer 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 sample code that uses localeconv to obtain and print the numeric and currency formatting information of the current locale:
Example
#include <locale.h>
int main() {
// Set locale information to the default settings from the user environment variables
setlocale(LC_ALL, "");
// Get locale information for the current locale
struct lconv *lc = localeconv();
// Print 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 currency 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 us 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 explanation
-
numeric format information:
decimal_point: Decimal point character, for example "."。thousands_sep: Thousands separator, for example ","。grouping: Number grouping.
-
currency format information:
int_curr_symbol: International currency symbol, e.g., "USD".currency_symbol: Local currency symbol, e.g., "$".mon_decimal_point: Currency decimal point character.mon_thousands_sep: Currency thousands separator.mon_grouping: Currency grouping.positive_sign: Positive sign, for example "".negative_sign: Negative sign, for example "-".int_frac_digits: International fractional digits.frac_digits: Local fractional digits.p_cs_precedes: Whether the currency symbol precedes a positive value (1 = precedes, 0 = follows).p_sep_by_space: Whether there is a space between the currency symbol and a positive value (1 = yes, 0 = no).n_cs_precedes: Whether the currency symbol precedes a negative value (1 = precedes, 0 = follows).n_sep_by_space: Whether there is a space between the currency symbol and a negative value (1 = yes, 0 = no).p_sign_posn: Positive sign position (1 = precedes, 2 = follows, 3 = adjacent to the numeric value, 4 = adjacent to the currency symbol).n_sign_posn: Negative sign position (1 = precedes, 2 = follows, 3 = adjacent to the numeric value, 4 = adjacent to the currency symbol).
Summary
localeconvThe function allows the program to obtain the numeric and currency 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 thesetlocaleandlocaleconvfunction, programmers can implement program localization, thereby meeting the needs of different user groups.