Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QJsonObject::insert use fixed instead of scientific notation when inserting double
Forum Updated to NodeBB v4.3 + New Features

QJsonObject::insert use fixed instead of scientific notation when inserting double

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 113 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dadde
    wrote on last edited by Dadde
    #1

    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.

    Christian EhrlicherC JonBJ 2 Replies Last reply
    0
    • D Dadde

      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.

      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      4
      • D Dadde

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        1
        • D Dadde has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved