C 库 - localeconv() 函数



C 库的 localeconv() 函数将 struct lconv 的组件设置为适合当前区域设置的值。该结构可能会被对 localeconv() 的另一次调用或调用 setlocale() 函数覆盖。

语法

以下是 C 库 localeconv() 函数的语法 -

struct lconv *localeconv(void)

参数

  • 此函数不接受任何参数。

返回值

此函数返回一个指向当前区域设置的 struct lconv 的指针,该指针具有以下结构 -

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

示例 1

以下是显示 localeconv() 函数用法的基本 C 程序。

#include <locale.h>
#include <stdio.h>

int main () {
   struct lconv * lc;

   setlocale(LC_MONETARY, "it_IT");
   lc = localeconv();
   printf("Local Currency Symbol: %s\n", lc -> currency_symbol);
   printf("International Currency Symbol: %s\n", lc -> int_curr_symbol);

   setlocale(LC_MONETARY, "en_US");
   lc = localeconv();
   printf("Local Currency Symbol: %s\n", lc -> currency_symbol);
   printf("International Currency Symbol: %s\n", lc -> int_curr_symbol);

   setlocale(LC_MONETARY, "en_GB");
   lc = localeconv();
   printf ("Local Currency Symbol: %s\n", lc -> currency_symbol);
   printf ("International Currency Symbol: %s\n", lc -> int_curr_symbol);

   printf("Decimal Point = %s\n", lc->decimal_point);
   
   return 0;
}

输出

以上代码产生以下结果 -

Local Currency Symbol: EUR
International Currency Symbol: EUR
Local Currency Symbol: $
International Currency Symbol: USD
Local Currency Symbol: £
International Currency Symbol: GBP
Decimal Point = .

示例 2

在下面的程序中,使用 localeconv() 函数检索数字格式信息。

#include <stdio.h>
#include <locale.h>

int main() {
   // Set the locale to the user's default locale
   setlocale(LC_ALL, "");

   // Get the numeric formatting information
   struct lconv *localeInfo = localeconv();
   
   printf("Following Outputs-");
   printf("Decimal separator: %s\n", localeInfo -> decimal_point);
   printf("Thousands separator: %s\n", localeInfo -> thousands_sep);
   printf("Currency symbol: %s\n", localeInfo -> currency_symbol);

   return 0;
}

输出

执行上述代码后,我们将得到以下结果 -

Following outputs-
Decimal separator: .
Thousands separator: ,
Currency symbol: $

示例 3

在这里,我们正在使用 localeconv() 访问日期和时间格式的信息。

#include <stdio.h>
#include <time.h>
#include <locale.h>

int main() {
    // Set the locale to the user's default locale
    setlocale(LC_ALL, "");

    // Get the current time
    time_t t = time(NULL);
    struct tm *tm_info = localtime(&t);

    // Buffer to hold the formatted date and time string
    char buffer[80];

    // Formatting of date and time according to the locale
    strftime(buffer, sizeof(buffer), "%c", tm_info);

    // Print the formatted date and time string
    printf("Formatted date and time: %s\n", buffer);

    return 0;
}

输出

编译上述代码后,我们将得到以下结果 -

Formatted date and time: Mon May 20 12:49:30 2024
广告