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

setprecision support #2362

Closed
1 of 3 tasks
young66 opened this issue Aug 19, 2020 · 5 comments
Closed
1 of 3 tasks

setprecision support #2362

young66 opened this issue Aug 19, 2020 · 5 comments
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@young66
Copy link

young66 commented Aug 19, 2020

What is the issue you have?

i try to run the follow code

#include <iostream>
#include <iomanip>
json testj;
testj["data1"] = 7.0000000001;//copy from var like 7.0
//some more data in json
testj["data2"] = 0.9999999999;//copy from var like 1.0
cout << setprecision(2);
cout << fixed;
cout << "precision=2 fix :" << 1.1111111111111 << ", json: " << testj<< endl;

Please describe the steps to reproduce the issue.

run the code

Can you provide a small but working code example?

yes ,see the sample

#include <iostream>
#include <iomanip>
json testj;
testj["data1"] = 7.0000000001;//copy from var with 7.0
//some more data in json
testj["data2"] = 0.9999999999;//copy from var with 1.0
cout << setprecision(2);
cout << fixed;
cout << "precision=2 fix :" << 1.1111111111111 << ", json: " << testj<< endl;

What is the expected behavior?

it should print
precision=2 fix :1.11, json: {"data1":7.00, etc... ,"data2":1.00}

And what is the actual behavior instead?

but the output is
precision=2 fix :1.11, json: {"data1":7.0000000001, etc... ,"data2":0.9999999999}
i try to test setw and setfill, it works; but doesn't work for setprecision

Which compiler and operating system are you using?

  • Compiler: gcc.7.3.0
  • Operating system: suse11.3

Which version of the library did you use?

  • latest release version 3.9.1
  • other release - please state the version: 3.7.3
  • the develop branch
@nlohmann
Copy link
Owner

It's not really a bug - the library has no option to change the output of floating-point numbers. If you use testj.get<double>() instead of testj, then it should work.

@young66
Copy link
Author

young66 commented Aug 20, 2020

sorry,i show this code ,just want to say setprecision doesn't work.
my code is like this, i have update the code in issue.

json testj;
testj["data1"] = 7.0000000001;//copy from var like 7.0
//some more data in json
testj["data2"] = 0.9999999999;//copy from var like 1.0
cout << setprecision(2);
cout << fixed;
cout << "precision=2 fix :" << 1.1111111111111 << ", json: " << testj<< endl;

it should print
precision=2 fix :1.11, json: {"data1":7.00, etc... ,"data2":1.00}

but the output is
precision=2 fix :1.11, json: {"data1":7.0000000001, etc... ,"data2":0.9999999999}

i think o.precision() should pass to the function operator<< in include/nlohmann/json.hpp
and use the precision to format the double/float var for function dump_float in single_include/nlohmann/json.hpp
like this

friend std::ostream& operator<<(std::ostream& o, const basic_json& j)
{
// read width member and use it as indentation parameter if nonzero
const bool pretty_print = o.width() > 0;
const auto indentation = pretty_print ? o.width() : 0;
// reset width to 0 for subsequent calls to this stream
o.width(0);

// do the actual serialization // <<-------o.fill() has passed to serializer, so do o.precision for double formating
serializer s(detail::output_adapter<char>(o), o.fill(), o.precision());
s.dump(j, pretty_print, false, static_cast<unsigned int>(indentation));
return o;
}

@stale
Copy link

stale bot commented Sep 20, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Sep 20, 2020
@stale stale bot closed this as completed Oct 4, 2020
@axtschmied
Copy link

Hello!

This topic has been closed due to inactivity, but it would be a very useful feature in my opinion as well.

@DinoMalpera
Copy link

We also need this. It would be very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

4 participants