Skip to content

dumping a small number_float just outputs 0.000000 #37

Closed

Description

@jrandall

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.

Activity

  1. jrandall commented on Mar 23, 2015

    @jrandall
    ContributorAuthor

    Any issues with this that I could address to get it merged?

  2. nlohmann commented on Mar 23, 2015

    @nlohmann
    Owner

    I was on holiday... I'll check this issue tonight.

  3. added a commit that references this issue on May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions