我正在逐行读取 CSV 并标记每个逗号分隔值。每个标记都是一个字符串类型。我将它放入浮点类型的向量中。在下面的示例中,例如,如果 csv 中的值为 "0.08" , *beg = "0.08" ,但在向量 v 中为 "0.079999998"
有没有办法可以将向量中的精度设置为小数点后 3 位或其他东西。
例子:
string line;
boost::char_separator<char> sep(",");
typedef boost::tokenizer< boost::char_separator<char> > t_tokenizer;
ifstream myfile (fileName);
if(myfile.is_open())
{
while (myfile.good())
{
getline (myfile,line);
t_tokenizer tok(line, sep);
for (t_tokenizer::iterator beg = tok.begin(); beg != tok.end(); ++beg)
{
string temp = *beg;
this->v.push_back(::atof(temp.c_str()));
}