printf输出不受全局语言环境影响吗? [英] printf output not affected by global locale?

查看:76
本文介绍了printf输出不受全局语言环境影响吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法在Visual C ++(VS 2005)中获得printf函数,无法以整数形式输出数千个分隔符.

I cannot get the printf functions in Visual-C++ (VS 2005) to output thousands separator in my integers.

注意:注释表明这是printf的正常行为.但是,C ++ iostream 是否将千位分隔符插入数字(已通过std::stringstreamimbue测试),所以这纯粹是iostream的功能,并且不存在于"C"格式函数中吗?

Note: Comments indicate that this is printf's normal behaviour. However, C++ iostream does insert the thousands separator into numbers (tested with std::stringstream and imbue) so is this purely a feature of iostreams and isn't present in the "C" formatting functions?

这是测试代码:

    setlocale(LC_ALL, ""); // -> User locale
    _locale_t loc = _create_locale(LC_ALL, ""); // -> user locale for *_l version

    struct lconv *pLocalSettings = localeconv();
    printf("Locale is: %s\n", setlocale(LC_NUMERIC, NULL));
    printf("Thousands separator set to : {%s}\n", pLocalSettings->thousands_sep);
    printf("Grouping set to: {%o}\n", int(pLocalSettings->grouping[0]));

    int i = 1000000;
    __int64 i64 = i * 2;
    printf("32 Bit integer output: %d\n", i);
    printf("64 Bit integer output: %I64d\n", i64);
    _printf_l("32 Bit integer output: %d\n", /*locale=*/loc, i);
    _printf_l("64 Bit integer output: %I64d\n", /*locale=*/loc, i64);
    _free_locale(loc);

输出为:

Locale is: German_Austria.1252
Thousands separator set to : {.}
Grouping set to: {3}
32 Bit integer output: 1000000
64 Bit integer output: 2000000
32 Bit integer output: 1000000
64 Bit integer output: 2000000

进一步检查后,似乎无法正常工作.我试过了:

Upon further checking, it would appear that it doesn't work like this. I tried this:

#include <iostream>
#include <clocale>
using namespace std;

int main() {
    setlocale(LC_ALL, "en_US"); // -> User locale

    struct lconv *pLocalSettings = localeconv();
    printf("Locale is: %s\n", setlocale(LC_NUMERIC, NULL));
    printf("Thousands separator set to : {%s}\n", pLocalSettings->thousands_sep);
    printf("Grouping set to: {%o}\n", int(pLocalSettings->grouping[0]));

    int i = 1000000;
    long long  i64 = i * 2;
    printf("32 Bit integer output: %d\n", i);
    printf("64 Bit integer output: %I64d\n", i64);

    return 0;
}

http://www.compileonline.com/compile_cpp11_online.php 上(不能在那里找不到永久链接.)

on http://www.compileonline.com/compile_cpp11_online.php (Can't find no permalink there.)

输出为:

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1

Executing the program....
$demo
Locale is: en_US
Thousands separator set to : {,}
Grouping set to: {3}
32 Bit integer output: 1000000
64 Bit integer output: 2000000

推荐答案

让我分享在这里学到的知识:

Let me share what I learned here:

  • C语言环境确实包含有关千位分隔符的信息,但此信息是
  • C locale does contain the information regarding the thousands separator, but this information is not used at all by the printf* family of functions (unless you count the flag extension ', which isn't present in MSVC anyways)
  • C++ locale contains the info as well, and iostreams actually make use of it when formatting numbers (numpunct facet)
  • Setting the global C locale with setlocaledoes not affect the global C++ locale set by std::locale::global()

获得我格式化的数字的正确代码最终是:

The correct code to get my formatted numbers ended up being:

static std::locale user_locale("");
std::wstringstream buf; 
buf.imbue(user_locale); 
buf << some_integer; 

  • 对于某些用例,可以通过 查看全文

  • 登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆