C 语言库函数 - localeconv()

返回上一级

C 语言标准库函数 struct lconv *localeconv(void) 用于设置或读取地域化信息。

localeconv 函数会返回一个 lconv 结构类型的对象。

头文件

#include <locale.h>

函数原型

下面是 localeconv() 函数的原型

struct lconv *localeconv(void)

参数

返回值

localeconv 函数返回一个指向当前区域 struct lconv 的指针。

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

每个字段解释请看 C 标准库 - <locale.h>

范例

下面的范例演示了 localeconv() 函数的用法

/**
 * file: main.c
 * author: 简单教程(www.twle.cn)
 *
 * Copyright © 2015-2065 www.twle.cn. All rights reserved.
 */

#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);

   printf("\n");

   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);

   printf("\n");

   setlocale(LC_MONETARY, "zh_CN");
   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;
}

编译运行范例,输出结果如下

$ gcc main.c && ./a.out
Local Currency Symbol: Eu
International Currency Symbol: EUR

Local Currency Symbol: $
International Currency Symbol: USD

Local Currency Symbol: ¥
International Currency Symbol: CNY
Decimal Point = .

返回上一级

C 语言标准库

关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.