c++ - 更改字符串语言环境

标签 c++ string stl

我对特定于语言环境的转换不是很熟悉,所以我在这里可能使用了错误的术语。这就是我想要发生的事情。

我想写一个函数

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

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

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

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

感谢您的帮助!

最佳答案

像这样的东西应该可以解决问题

#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++ 上如此)。您可能不得不摆弄语言环境规范,因为它非常特定于平台。

关于c++ - 更改字符串语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1986199/

相关文章:

c++ - "types"点的强类型解决方案

c++ - 从远程计算机连接到 mssql 服务器会导致用户 '' 登录失败

c++ - unordered_map C++

c++ - c++ 中 myoutput << myinput.rdbuf() 之类的 iostream 语句的语义是什么?

c++ - 'switch' 在 C/C++ 中有哪些有趣的用途?

c++ - 使用字符串类输入空格时出现 cin 问题

c# - 当数据可以包含逗号时在逗号上拆分字符串

c++ - 为什么可以使用 'string' 作为变量名

c++ - 嵌套循环中迭代器的行为与 C++ 中的随机访问

c++ - 在 Qt 中编写 SQlite 数据库