Custom Precision on floating point numbers
Author: DarkZerosCreated Jul 19, 2018Updated Sep 28, 2026
Labelsstate: waiting for PR
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}
Source: nlohmann/json