3

I'm writing a simple tool in Qt which reads data from two GPX (XML) files and combines them in a certain way. I tested my tool with track logs that contain waypoints having 6 decimal digits precision. When I read them from the GPX file, the precision gets reduced to 4 decimal digits (rounded properly). So for example this original tag:

<trkpt lat="61.510656" lon="23.777735">

turns into this when my tool writes it again:

<trkpt lat="61.5107" lon="23.7777">

Debug output shows the precision loss happens on this line:

double lat = in.attributes().value("", "lat").toString().toDouble();

but I can't see why. in is a QXmlStreamReader reading from a text file handle.

teukkam
  • 4,267
  • 1
  • 26
  • 35

1 Answers1

6

It is probably when you are writing the value back to the XML. Please post that code in your question.

If I had a guess before seeing the code, you are using QString::number to convert from the double back to a string. The default precision in the conversion is 6, which corresponds to what you are seeing. You can increase the precision to get all the decimals.

Dave Mateer
  • 17,608
  • 15
  • 96
  • 149
  • I put this: qDebug() << "Found wpt" << lat << "," << lon; after the above line and it show lat and lon are already rounded to 4 digits. Unless, of course, qDebug also rounds with the same precision. – teukkam Jan 24 '12 at 15:01
  • 2
    Try: `qDebug() << QString::number(lat, 'f', 10)` – Dave Mateer Jan 24 '12 at 15:02
  • 1
    I'll bet everything eventually uses `QString QString::number(double n, char format = 'g', int precision = 6 )` – MSalters Jan 24 '12 at 15:07
  • That's the ticket! QString::number was indeed the culprit. – teukkam Jan 24 '12 at 15:12