-2

I want to convert QString("3.285072743893E-04") to double without compromising the precision.

When I use QString::toDouble the result is 0.000328507.

I have read somewhere that it is just a matter of how the value is displayed.

Does that mean, that converting the value to double, QString::toDouble actually preserves its precision, i.e. that the precission of the returned value of type double is not compromised? And more importantly, if we process the data, do we need to worry about its precision?

scopchanov
  • 7,966
  • 10
  • 40
  • 68
Archiac Coder
  • 156
  • 2
  • 13

1 Answers1

3

The output of executing

QString s1 = "3.285072743893E-04";
double num = s1.toDouble(NULL);
QString s2 = QString::number(num, 'g', 20);
std::cout << s2.toUtf8().constData() << std::endl;

on my setup (Qt 4.7.3) is:

0.00032850727438929998173

As you can see, the precision is retained for all but the last digit.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    in fact precision is fully conserved as much as `double` type allows - 15-16 decimal digits. So when you take 17 significant digits `0.00032850727438929998` and round them to 16 digits you will have exactly same value as initially. – Marek R Sep 15 '18 at 20:54