C localeconv()

localeconv() 函数定义在 locale.h 头文件中。它有助于设置或读取当前程序的与位置相关的信息。这些信息以 lconv 结构体类型的对象返回,该对象表示当前 C 语言环境的数字和货币格式规则。


struct lconv *localeconv(void) 
 

localeconv() 参数

setlocale() 函数不接受任何参数。localeconv() 返回的对象不能被程序更改,因为它会被 setlocale() 函数覆盖。

localeconv() 返回值

它返回一个指向静态对象的指针,该对象包含当前 C 语言环境的格式规则,并具有以下结构。


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
 
输入 返回值
成功时 指向静态对象的指针
失败时 空指针

localeconv() 方法在 Python 中的示例

示例 1:localeconv() 函数在 C 语言中的工作原理


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

int main () {
   
 struct lconv * loc;

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

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

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

   printf("Decimal Point = %s\n", loc->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() 函数在 C 语言中如何工作?


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

int main(int argc, const char * argv[])
{
    
    struct lconv *loc;

    /* Set the locale to the environment default */
    setlocale (LC_ALL, "");

    /* Retrieve a pointer to the current locale */
    loc = localeconv();

 
    printf("Thousands Separator: %s\n", loc->thousands_sep);
    printf("Currency Symbol: %s\n", loc->currency_symbol);

    return 0;
}

输出


Thousands Separator: ,
Currency Symbol: $