[Qt-interest] QVariant imprecision?

Julien Cugnière julien.cugniere at gmail.com
Tue Feb 16 14:46:56 CET 2010


2010/2/16 Mario Signorino <mario.signorino at gmail.com>:
>    double doubleFrom = 99999.99;
>    QVariant var(doubleFrom);
>    double doubleTo = var.toDouble();
>    qDebug() <<"doubleTo"<< doubleTo;
>    qDebug() <<"todouble"<< var.toDouble();
>    qDebug() <<"tostring"<< var.toString();
>    qDebug() <<"tofloat"<< var.toFloat();
>
> The output  (Qt 4.6.0 OpenSource):
>
>    doubleTo 100000
>    todouble 100000
>    tostring "99999.99"
>    tofloat 100000

The lack of precision comes not from QVariant, but from qDebug :

qDebug() << "doubleFrom" << doubleFrom; // gives 100000
qDebug() << QString::number(var.toDouble()); // gives 99999.99

I think this is because operator<< on qDebug() rounds doubles to 6
digits total. Try using QString::number(), or something like
qDebug("%f", d).

-- 
Julien Cugnière




More information about the Qt-interest-old mailing list