代码之家  ›  专栏  ›  技术社区  ›  Praetorian Luchian Grigore

更改字符串区域设置

  •  4
  • Praetorian Luchian Grigore  · 技术社区  · 15 年前

    我不太熟悉特定于区域设置的转换,因此可能使用了错误的术语。这就是我想要发生的事情。

    我想写一个函数

    std::string changeLocale( const std::string& str, const std::locale& loc )
    

    这样,如果我按如下方式调用此函数:

    changeLocale( std::string( "1.01" ), std::locale( "french_france" ) )
    

    输出字符串将为“1,01”

    谢谢你的帮助!

    1 回复  |  直到 8 年前
        1
  •  6
  •   Jeff Foster    15 年前

    像这样的东西应该会起作用的

    #include <iostream>
    #include <sstream>
    #include <locale>
    int main (int argc,char** argv) {
        std::stringstream ss;
        ss.imbue(std::locale("fr_FR.UTF8"));
        double value = 1.01; 
        ss << value; 
        std::cout << ss.str() << std::endl; 
        return 0;
    }             
    

    应该给你输出 1,01 (至少在G++上是这样的)。您可能需要修改区域设置规范,因为它非常特定于平台。