給我雙倍的錯誤的精度。

[英]QString::toDouble() giving me double with wrong precision


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

我有一個QString myNumber包含“09338.712001”。當我做:

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.

myNumber.toDouble();它返回9338.71,但我想要將double值作為原始值,即09338.712001。有沒有人知道如何讓double返回的精度與QString的精度相同?謝謝。

1 个解决方案

#1


12  

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

但不管怎樣,也許現在是讀這篇文章的好時機:每個計算機科學家都應該知道浮點運算。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/04/24/4726bd44896dfb384a1c2c0973bf2c51.html



 
  © 2014-2022 ITdaan.com