我想创建一个运算符<<使用本地setings,如果不是,至少可以手动改变使用"." 小数分隔符为",".我想要一种方法来使流(iostream,fstream等)执行此操作而不是创建字符串然后打印它.
这可能吗?
0x4*_*2D2 13
你可以在你的小溪上加入一个小小的方面.我相信这样的事情对你有用:
template <typename T>
struct comma_separator : std::numpunct<T>
{
typename std::numpunct<T>::char_type do_decimal_point() const
{
return ',';
}
};
template <typename T>
std::basic_ostream<T>& comma_sep(std::basic_ostream<T>& os)
{
os.imbue(std::locale(std::locale(""), new comma_separator<T>));
return os;
}
int main()
{
std::cout << comma_sep << 3.14; // 3,14
}
Run Code Online (Sandbox Code Playgroud)
一个较短的解决方案,它使用欧洲语言环境:
std::cout.imbue(
std::locale(
std::cout.getloc(), new std::numpunct_byname<char>("de_DE.utf8")));
Run Code Online (Sandbox Code Playgroud)
但最终它取决于系统提供的语言环境.