Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dumping a small number_float just outputs 0.000000 #37

Closed
jrandall opened this issue Feb 28, 2015 · 2 comments
Closed

dumping a small number_float just outputs 0.000000 #37

jrandall opened this issue Feb 28, 2015 · 2 comments

Comments

@jrandall
Copy link
Contributor

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.

@jrandall
Copy link
Contributor Author

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

@nlohmann
Copy link
Owner

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

GerHobbelt pushed a commit to GerHobbelt/nlohmann-json that referenced this issue May 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants