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. How to save double value as 0.0 instead of 0 from QJsonDocument to a file
Forum Updated to NodeBB v4.3 + New Features

How to save double value as 0.0 instead of 0 from QJsonDocument to a file

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 119 Views
  • 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.
  • N Offline
    N Offline
    niks_entire
    wrote on last edited by
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • N niks_entire

      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.

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

      @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 output 0.0 for a number.

      1 Reply Last reply
      2

      • Login

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