2

If I do:

  auto jsonValue = QJsonValue(0.1);
  auto jsonObj = QJsonObject();
  jsonObj.insert("Key", jsonValue);
  QJsonDocument jsonDoc(jsonObj);    
  auto json = jsonDoc.toJson();    
  std::cout << json.toStdString() << std::endl;

I get:

{
    "Key": 0.10000000000000001
}

But I would like:

{
    "Key": 0.1
}

Is this at least possible with Qt?

I use Qt 5.6.

I works fine for Qt 5.9 and Qt 5.11. Not tested with 5.10 but it should also work hopefully.

PS: I know why 0.1 becomes 0.10000000000000001

Korchkidu
  • 4,908
  • 8
  • 49
  • 69
  • 1
    What if you use string values instead? I.e. `auto jsonValue = QJsonValue("0.1");` – vahancho Nov 28 '18 at 12:33
  • 2
    @vahancho I think the output will be `"Key": "0.1"` instead of `"Key": 0.1` – eyllanesc Nov 28 '18 at 12:34
  • @eyllanesc, right. That's what I meant. When read this value, it can be converted to double. But I don't know whether OP is bound to particular JSON scheme. – vahancho Nov 28 '18 at 12:36
  • What is your Qt version? I am using Qt5.11.2 msvc x64, and I get 0.1 in debug and release. – 张铁男 Nov 28 '18 at 12:38
  • @eyllanesc std::cout – 张铁男 Nov 28 '18 at 12:40
  • If you already know why 0.1 becomes 0.10000000000000001 (i.e., the fraction 1/10 is impossible to represent in binary floating point), then surely you're already aware that any decimal stringification will necessarily be an approximation. @vahancho has the right idea: serialize your numbers as strings! – Pedro LM Nov 28 '18 at 12:40
  • I just tested and as noted @张铁男 I get 0.1 :-) – eyllanesc Nov 28 '18 at 12:42
  • IMO this is bug in Qt (it tries to print value beyond `double` precision), check if it is not already reported. – Marek R Nov 28 '18 at 12:49
  • @TheQuantumPhysicist this is not a duplicate since it is different library. Here you have Qt, and the other is Jsoncpp. – Marek R Nov 28 '18 at 12:51
  • @MarekR This is a json issue, not a library issue. I asked about the exact same problem. – The Quantum Physicist Nov 28 '18 at 12:52
  • @vahancho: yes, I need to send a double value, not a string. – Korchkidu Nov 28 '18 at 12:53
  • Qt version is 5.6. – Korchkidu Nov 28 '18 at 12:55
  • @TheQuantumPhysicist can you explain why this is JSon issue and not library implementation issue? Note that Qt tries print value beyond precision of `double` type (it tires to print 17 significant digits), so it is clearly library bug. – Marek R Nov 28 '18 at 12:56
  • @PedroLM: it may be possible in Qt to use something like QByteArray::number(0.1, 'f', 4) and inject it in my QJsonObject. But I didn't find the solution yet. – Korchkidu Nov 28 '18 at 12:58
  • 2
    @MarekR Because according to the json standard, doubles are doubles and can't be interpreted otherwise. So there's no way around it. You may be lucky to find an implementation that deviates from there, but then that's not standard json. – The Quantum Physicist Nov 28 '18 at 12:58
  • @TheQuantumPhysicist: I really do not care about the standard actually. I just want my 0.1 to be serialized as 0.1. It seems actually that Qt 5.11 may take this into account though. Only problem is: I need to stick with 5.6. – Korchkidu Nov 28 '18 at 13:00
  • @TheQuantumPhysicist I don't get it `0.1` is a `double` too which will properly represent this value. It should not be represented as `0.10000000000000001` since it doesn't provide any extra information. – Marek R Nov 28 '18 at 13:02
  • @Korchkidu Qt uses the standard for it returns the decimals, in the standard does not indicate the decimals of precision so a possible solution is to parse the QString that returns toJson () and eliminate those decimals. – eyllanesc Nov 28 '18 at 13:08
  • 2
    @MarekR: 0.1 cannot be represented using floating-point representation. – Korchkidu Nov 28 '18 at 13:10
  • @eyllanesc: yes, that may be a good solution. – Korchkidu Nov 28 '18 at 13:11
  • @TheQuantumPhysicist: this is not a duplicate as it is specific to Qt. I want to find a solution to "inject" 0.1 in the final QJsonDocument. – Korchkidu Nov 28 '18 at 13:15
  • @Korchkidu I know that OP knows that. My point is that parsing `0.1` and `0.10000000000000001` to `double` leads to exactly same representation. There is no point to print this value with such precision since it doesn't cray any extra information! That is why I'm claiming it is Qt bug. When value is printed the outcome should be limited to something whne parsed will restore original `double` value. `0.1` is fulfilling this requirement. – Marek R Nov 28 '18 at 14:25
  • @MarekR: you are actually right. It is a Qt bug. It has been fixed from (at least) Qt 5.9. – Korchkidu Nov 29 '18 at 13:21

0 Answers0