Search code examples
c++jsonrapidjson

Set floating point precision using rapidjson


Is there a way to control the output precision in JSON generated using rapidjson?

For example:

writer.String("length");
writer.Double(1.0 / 3.0);

This generates something like:

{ length: 0.33333333 }

I'm sending a lot of values and only need two decimal places for several values.


Solution

  • For anyone stumbling onto this case, this method was added to rapidjson on Feb 11, 2016:

    rapidjson::Writer::SetMaxDecimalPlaces(int maxDecimalPlaces)

    The provided default Writer class is more complete than the other answer here, and should always be used, unless you have very specific needs.