我需要打印一个带有数字的cvs文件。 打印文件时,我有带点的数字,但我需要用逗号。C++如何在文件中用逗号(而不是点)打印一个双精度的十进制数字
这里是一个例子。 如果我使用语言环境方法在终端打印此号码,我获得一个逗号的数字,但在文件中我有相同的号码,但点。我不懂为什么。 我该怎么办?
#include <iostream>
#include <locale>
#include <string> // std::string, std::to_string
#include <fstream>
using namespace std;
int main()
{
double x = 2.87;
std::setlocale(LC_NUMERIC, "de_DE");
std::cout.imbue(std::locale(""));
std::cout << x << std::endl;
ofstream outputfile ("out.csv");
if (outputfile.is_open())
{
outputfile <<to_string(x)<<"\n\n";
}
return 0;
}
在此先感谢。
灌输物流对象,而不是cout。 –
@尼尔[似乎没有帮助](http://coliru.stacked-crooked.com/a/2947e8488c8fb6a2)。 –
请注意,您需要为'std :: setlocale'包含''。它可能在没有头文件的情况下工作,但不能保证(例如,如果没有Visual C++,它不会编译)。 –