Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have made class that convert
QList<qreal>
(very big list) to JSON string, but it generates a extra large text.
Here is code:
QJsonObject rootObject;
rootObject.insert("Test",0.05);
qDebug()<<QJsonDocument(rootObject).toJson(QJsonDocument::Compact);
And I have tried equal code:
QJsonObject rootObject;
rootObject.insert("Test",QString("0.05").toDouble());
qDebug()<<QJsonDocument(rootObject).toJson(QJsonDocument::Compact);
And debug ouptut is always:
{"Test":0.050000000000000003}
I want to get short output like this:
{"Test":0.05}
Is there way to fix QJsonDocument
? Or make some decimals count rounding/limit?
–
–
This prints out 0.5, as expected.
However, I think the problem is due to floating point precision. QJSonDocument is representing the number as accurately as possible, but does not have a function to limit the number of decimal places represented, as is present in QString.
Though not ideal, if your really want 0.5 represented this way, you can write a string value instead of the double.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.