Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QString::toDouble() giving me double with wrong precision

I have a QString myNumber containing "09338.712001". When I do:

myNumber.toDouble();, it returns 9338.71, but I want the double to be the original value, which is 09338.712001. Does anyone know how to get the double returned by toDouble to have the same precision as the QString? Thanks.

like image 572
user446836 Avatar asked Apr 24 '12 14:04

user446836


1 Answers

Your problem probably is in how you output these values.

 QString s("9338.712001");
 bool ok = false;

 double a = 9338.712001;
 double b = s.toDouble(&ok);
 double c = 1/3.0;

 qDebug() << "a: " << a;
 qdebug() << "b: " << b;

 qDebug() << "a: " << QString("%1").arg(a, 0, 'g', 13) 
 qDebug() << "b: " << QString("%1").arg(b, 0, 'e', 13);

 qDebug() << "c: " << QString("%1").arg(c, 0, 'g', 30);

result:

 a:  9338.71  
 b:  9338.71  
 a:  "9338.712001"
 b:  "9.3387120010000e+03"
 c:  "0.333333333333333314829616256247"

But anyways, maybe now it's a good moment to read this: What Every Computer Scientist Should Know About Floating-Point Arithmetic

like image 57
Diego Schulz Avatar answered Sep 19 '22 14:09

Diego Schulz