Skip to content

dumping a small number_float just outputs 0.000000 #37

Closed
@jrandall

Description

I expected that small doubles would be represented in dumped JSON as a javascript number in scientific notation, but instead it just appears as 0.000000

Example:

#include <iostream>

#include "json.hpp"
using json = nlohmann::json;

main() {
  double d = 1.23456e-89;
  json j = d;

  std::cout << "d=" << d << " j=" << j << std::endl;
}

Expected output: d=1.23456e-89 j=1.23456e-89

Actual output: d=1.23456e-89 j=0.000000

Looking at the json code, it appears this is due to using std::to_string() for all numbers, which is equivalent to sprintf("%f", ...) whereas sprintf("%g", ...) would be more appropriate. To fix this, the dump function would need to use something other than to_string to format floating point numbers. I'll try to put together a PR for this although C++ is far from my best language.

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions