使用std :: use_facet获取本地货币

时间:2017-07-31 00:58:24

标签: c++ c++-standard-library

我正在尝试使用标准库来根据区域设置决定结算信息。我讲了几个例子http://en.cppreference.com/w/cpp/locale/use_facet

根据示例,它返回USD但是当我跑步时我什么也得不到

#include <iostream>
#include <locale>

int main()
{
    std::locale loc = std::locale(""); // user's preferred locale
    std::cout << "Your currency string is "
              << std::use_facet<std::moneypunct<char, true>>(loc).curr_symbol() << '\n';
}

输出

Your currency string is 
Program ended with exit code: 0

预期输出(如果是美国用户)

Your currency string is USD 
Program ended with exit code: 0

预期产出(如果来自印度的用户)

Your currency string is RS  
Program ended with exit code: 0

我错过了什么吗?如何使其默认使用基于用户时区的语言环境?

1 个答案:

答案 0 :(得分:0)

突破

#include <iostream>
#include <locale>
#include <clocale>


int main()
{
    std::setlocale(LC_MONETARY, "it_IT");
    const struct lconv * const currentlocale = localeconv();
    std::cout<<"In the current locale, the default currency symbol is:"<<currentlocale->currency_symbol<<std::endl;

}

输出

在当前区域设置中,默认货币符号为:Eu

程序以退出代码结束:0