[Qt-interest] qDebug() and double values

Sean Harmer sean.harmer at maps-technology.com
Mon Dec 21 09:06:55 CET 2009


Hi,

On Monday 21 December 2009 03:55:56 Phil wrote:
> Thank you for reading this.
> 
> I'm trying to trace numbers similar to the following but qDebug() rounds
> them to integers.
> 
> double d = 435850.925;
> qDebug() << "d = " << d;
> 
> 435851 is displayed.
> 
> However a smaller number, such as the following, is displayed correctly.
> 
> d = 12.125;
> qDebug() << "d = " << d;
> 
> 12.125 is displayed.
> 
> Is qDebug() able to display six digit numbers with, say, three digits after
> the decimal point?

Try something like this:

qDebug() << QString::number( d, 'f', 3 );

>From the QString docs:

"A precision is also specified with the argument format. For the 'e', 'E', and 
'f' formats, the precision represents the number of digits after the decimal 
point. For the 'g' and 'G' formats, the precision represents the maximum 
number of significant digits (trailing zeroes are omitted)."

So you can mix and match as appropriate rather than leaving qDebug() to its 
default behaviour.

Or you could patch the QDebug operator << ( QDebug, const QString& ) function 
to do what you need.

Sean




More information about the Qt-interest-old mailing list