Skip to content

Custom Precision on floating point numbers #1170

@DarkZeros

Description

@DarkZeros

When storing floating point values, exact representation is not possible.
ie: -0.2 -> -0.019999995827674866

However, when precision is not a big issue this introduces huge about of bloat in the JSON file. So there should be an option to specify how many characters of precision. Like it is possible in std::ostringstream (std::ostreams), and in printf.

Currently these 2 methods would be desired:

json j;
j["float"] = -0.123;
j.precision(2);
cout << j;
json j;
j["float"] = -0.123;
j.precision(2);
printf("%s", j.dump().c_str());

Desired: {"float":-0.12}
Currently: {"float":-0.12300000339746475}

Metadata

Metadata

Assignees

No one assigned

    Labels

    state: stalethe issue has not been updated in a while and will be closed automatically soon unless it is updated

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions