Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QJsonObject insert QJsonValue(double) looses precision
snoopy24 last edited by
I have a double value which i want to bring into a QJsonObject. But on inserting the QJsonValue into the QJsonObject the precision of the value changed. But why
double x = 23.2;
QJsonValue v = QJsonValue(x);
qDebug() << x; qDebug() << v; qDebug() << obj;
What's going wrong here. Can anybody help me pls?
Hi, and welcome to the Qt Dev Net!
There are two things you need to be aware of:
Your loss of precision occured when you assigned 23.2 to x. A
double, as defined by the "IEEE floating point standard":http://en.wikipedia.org/wiki/IEEE_floating_point, cannot store 23.2 precisely.
The different outputs you saw is due to "roundings" that qDebug() did before displaying the text. All 3 values are actually the same.
To check yourself, do this:
qDebug() << obj;
qDebug() << obj.value("value");
qDebug() << obj.value("value").toDouble();