如何使浮点使用逗号而不是点? [英] How to make that a float use comma and not point?
本文介绍了如何使浮点使用逗号而不是点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想让操作员<<使用本地设置或如果不是至少手动能够改变。的使用。将小数分隔符改为,。我想要一种方法使流(iostream,fstream等)这样做,而不是创建字符串,然后打印它。
I want to make a operator<< that use the local setings or if not at least manualy be able to change the use of "." for decimal separator to ",". I will like a way of making that the stream (iostream, fstream, etc) to do this and not to create the string and then print it.
这是可能吗?
推荐答案
您可以在数据流中插入一个数字。我相信这样的东西应该为你工作:
You can imbue a numpunct facet onto your stream. I believe something like this should work for you:
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
}
使用欧洲地区:
std::cout.imbue(
std::locale(
std::cout.getloc(), new std::numpunct_byname<char>("de_DE.utf8")));
但最终取决于系统提供的语言环境。
But ultimately it depends on the locales that your system provides.
这篇关于如何使浮点使用逗号而不是点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文