C++ 如何使浮点使用逗号而不是点?
我想做一个操作符您可以将numpunt方面嵌入到您的流中。我相信这样的事情应该适合你:C++ 如何使浮点使用逗号而不是点?,c++,visual-c++,C++,Visual C++,我想做一个操作符您可以将numpunt方面嵌入到您的流中。我相信这样的事情应该适合你: template <typename T> struct comma_separator : std::numpunct<T> { typename std::numpunct<T>::char_type do_decimal_point() const { return ','; } }; template <typenam
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::numpunt
{
typename std::numpunct::char\u type do\u decimal\u point()常量
{
返回“,”;
}
};
模板
std::basic_ostream和逗号_sep(std::basic_ostream和os)
{
imbue(std::locale(std::locale(“”,新的逗号分隔符));
返回操作系统;
}
int main()
{
std::难道你不是说“逗号”而不是“冒号”?另外,“,”是逗号,不是冒号。@Pete否默认值是“C”
区域设置。你可以使用空字符串作为名称将其更改为用户首选的区域设置,例如std::区域设置::全局(std::区域设置(“”);
(+嵌入流)你的意思是你想做一些类似于out的事情。使用与操作系统区域设置相对应的facet不是比使用定制的facet更好吗?@MarkRansom视情况而定-在这种情况下不会直接回答OP的问题-如果他在美国并且想*让我们美国人看到逗号怎么办?谁知道真相intent@MarkRansom什么在这种情况下,我会更改代码吗?会将os.getloc()
更改为std::locale(“”)
do it?@0x499602D2我没有足够的区域设置经验,无法100%肯定地说,但您对这个问题有一些好的评论,这可能会有所帮助。请注意,这将永久性地改变std::cout
的行为。如果不希望这样做,您可以使用std::ostream自定义\u创建自己的ostream
(std::cout.rdbuf());
。然后,custom\u out
有自己的格式和区域设置,但使用与std::cout
相同的输出目标。
std::cout.imbue(
std::locale(
std::cout.getloc(), new std::numpunct_byname<char>("de_DE.utf8")));