- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
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
廣告