简体   繁体   中英

Reading values from text files without losing precision in Qt

I have a data file of x and y coordinates, and I'm trying to read them into two double vectors in Qt. My problem is that the values are being cut off or rounded off after 3 decimal places, even though my original values go up to 6. My data file looks like

35.659569 139.723370
35.659546 139.723194
35.659527 139.723051
35.659523 139.722909
35.659383 139.722946

My code is

QVector<double> v, v2;
QFile textFile (":/new/files/02262017newdata.txt");
if(textFile.open(QIODevice::ReadOnly))
{
  qInfo() << "opened file successfully";
  double a, b;
  QTextStream textStream (&textFile);
  while (!textStream.atEnd()) {
      QString line = textFile.readLine();
      QStringList list = line.split(" ");
      if(list.size() == 2){
        a = list.at(0).toDouble();
        b = list.at(1).toDouble();
      }
        qInfo() << "a and b after using split is" << a <<" "<< b;
        v.append(a);
        v2.append(b);
   }
}

How can I read the values without losing precision?

You're not losing precision. Problem is in qInfo() . Try setting precision in qInfo() using qSetRealNumberPrecision()

Replace this line:

qInfo() << "a and b after using split is" << a <<" "<< b;

with:

qInfo() << "a and b after using split is"<< qSetRealNumberPrecision(8) << a <<" "<< b;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM