How to save double value as 0.0 instead of 0 from QJsonDocument to a file
-
Hi,
I have a QJsonDocument containing some QJsonObjects - ints, doubles, and strings. The problem that I'm facing is when I try to save a double value as 0.0, it always get saved as 0 in the file and there is no decimal. It is some weird requirement to have but this is what I want to acheive basically.
QJsonDocument jsonDoc; QJsonObject jsonObj; jsonObj["radius"] = 0.0; jsonDoc.setObject(jsonObj); QSaveFile file; file.setFileName(QString::fromStdString(fileName)); if (!file.open(QIODevice::WriteOnly)) { return ERR_FAILURE; } file.write(jsonDoc.toJson()); file.commit();
In the file, I get 0 instead of 0.0.
-
Hi,
I have a QJsonDocument containing some QJsonObjects - ints, doubles, and strings. The problem that I'm facing is when I try to save a double value as 0.0, it always get saved as 0 in the file and there is no decimal. It is some weird requirement to have but this is what I want to acheive basically.
QJsonDocument jsonDoc; QJsonObject jsonObj; jsonObj["radius"] = 0.0; jsonDoc.setObject(jsonObj); QSaveFile file; file.setFileName(QString::fromStdString(fileName)); if (!file.open(QIODevice::WriteOnly)) { return ERR_FAILURE; } file.write(jsonDoc.toJson()); file.commit();
In the file, I get 0 instead of 0.0.
@niks_entire
You cannot do this. Unless you save it as a string,"0.0"
, but that's a different data type.JSON has only one numerical type, "number". This covers integers and floating point. You cannot control how it outputs the number.
0
will be its representation for the number with value zero, you cannot make it output0.0
for a number.