QJsonObject::insert use fixed instead of scientific notation when inserting double
-
QT version: 5.15
Hello, sorry to bother with this issue I have.
I am trying to insert a double variable into a QJsonObject, but I need it to not use special characters like the scientific notation.
Most solutions talks about using the std::fixed for streams, which I have issues with translating to my issue.
Alternative is to use QString::number, but again I need the object to not be a string of that fashion. If I use QString::number I will get a string instead with quotation marks surrounding the number.
Here is an example of my code:
QBytearray jsonDoc; valueObject = QJsonObject(); double myVariable = 0.0000001; valueObject.insert("Value", myVariable); jsonDoc = QJsonDocument(valueObject).toJson();result is:
{
"Value": 1e-07
}I want:
{
"Value": 0.0000001
}I would even be ok with rounding to 0:
{
"Value": 0.0
}I could round the value so that minimum allowed is 0.001, that way I should avoid the scientific notation. But I'm hoping there is a better solution to my problem?
Thanks in advance!
Edit: I'm marking this as solved this with Christian Erlichers answer. I believe my issue is somewhere at the other end where I send the data as a bytearray instead of a json to a server which will complain when it finds a scientific notation. This part is outside of QT so I will mark this thread as solved, the answer is that scientific notation should be allowed in JSON structures. I will have to dig deeper into this when I have time.
-
QT version: 5.15
Hello, sorry to bother with this issue I have.
I am trying to insert a double variable into a QJsonObject, but I need it to not use special characters like the scientific notation.
Most solutions talks about using the std::fixed for streams, which I have issues with translating to my issue.
Alternative is to use QString::number, but again I need the object to not be a string of that fashion. If I use QString::number I will get a string instead with quotation marks surrounding the number.
Here is an example of my code:
QBytearray jsonDoc; valueObject = QJsonObject(); double myVariable = 0.0000001; valueObject.insert("Value", myVariable); jsonDoc = QJsonDocument(valueObject).toJson();result is:
{
"Value": 1e-07
}I want:
{
"Value": 0.0000001
}I would even be ok with rounding to 0:
{
"Value": 0.0
}I could round the value so that minimum allowed is 0.001, that way I should avoid the scientific notation. But I'm hoping there is a better solution to my problem?
Thanks in advance!
Edit: I'm marking this as solved this with Christian Erlichers answer. I believe my issue is somewhere at the other end where I send the data as a bytearray instead of a json to a server which will complain when it finds a scientific notation. This part is outside of QT so I will mark this thread as solved, the answer is that scientific notation should be allowed in JSON structures. I will have to dig deeper into this when I have time.
@Dadde said in QJsonObject::insert use fixed instead of scientific notation when inserting double:
but I need it to not use special characters like the scientific notation.
Then don't use json.
The scientific notation is valid so your parser does not follow the rules.
See https://www.json.org/json-en.html -
QT version: 5.15
Hello, sorry to bother with this issue I have.
I am trying to insert a double variable into a QJsonObject, but I need it to not use special characters like the scientific notation.
Most solutions talks about using the std::fixed for streams, which I have issues with translating to my issue.
Alternative is to use QString::number, but again I need the object to not be a string of that fashion. If I use QString::number I will get a string instead with quotation marks surrounding the number.
Here is an example of my code:
QBytearray jsonDoc; valueObject = QJsonObject(); double myVariable = 0.0000001; valueObject.insert("Value", myVariable); jsonDoc = QJsonDocument(valueObject).toJson();result is:
{
"Value": 1e-07
}I want:
{
"Value": 0.0000001
}I would even be ok with rounding to 0:
{
"Value": 0.0
}I could round the value so that minimum allowed is 0.001, that way I should avoid the scientific notation. But I'm hoping there is a better solution to my problem?
Thanks in advance!
Edit: I'm marking this as solved this with Christian Erlichers answer. I believe my issue is somewhere at the other end where I send the data as a bytearray instead of a json to a server which will complain when it finds a scientific notation. This part is outside of QT so I will mark this thread as solved, the answer is that scientific notation should be allowed in JSON structures. I will have to dig deeper into this when I have time.
@Dadde
As @Christian-Ehrlicher says. To be clear, you cannot influence how the JSON output is written (nor how it is read) in the JSON producers/consumers I know. They only may allow you to influence the irrelevant spacing/indentation/newlines output format, like enum QJsonDocument::JsonFormat. -
D Dadde has marked this topic as solved on