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
廣告

© . All rights reserved.