im actually working with QT 5, using QJsonDocument, I need to save correctly formatted double into my Json Document, i tried several ways to format correctly, this is the way I ended using :
static double format(double value) {
double rounded = std::round(value * 100.0) / 100.0;
double r = QString::number(rounded, 'f', 2).toDouble();
return r;
}
It does work correctly, but then, when I set my values, and i create the document, when i log it, all the decimals values have changed.
For example :
jsonObject["amount"] = format(px); /* px = 1.90 */
QJsonDocument jsonDocument(jsonObject);
qWarning() << "Built document: " << jsonDocument.toJson(QJsonDocument::Indented);
/* In the log, amount is = 1.8999999999999999 */
I understand why it does that, "1.90 can't be stored in memory"
But i can't figure how to work around this. I can't store values in cents (i have to use double)
I also can't put strings in the json, the documentation/api im using is really really strict.
Im thinking about not using QJsonObject/Document/Array at all, and to use a minimalistic json parser, so i'll save and manipulate strings only.
Any help would be appreciated, thanks.